I have been struggling with Intents a little but I keep getting an error in this code.I keep getting a FATAL EXCEPTION..
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bac1.problemcodereference/com.example.bac1.problemcodereference.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
Code of my project:
public class MainActivity extends AppCompatActivity implements Serializable {
private String cPcode;
Button help , sub;
EditText codetext;
Intent intent = new Intent (this, Main2Activity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sub = (Button) findViewById(R.id.submit);
help = (Button) findViewById(R.id.help);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = codetext.getText().toString();
MainActivity onew = new MainActivity();
onew.setcPcode(text);
intent.putExtra("stuff",onew);
startActivity(intent);
}
});}
At this line
Intent intent = new Intent (this, Main2Activity.class);
your activity is not completely constructed so use this call after or inside oncreate function because this mean context which will be null hence the exception
So it will be
public class MainActivity extends AppCompatActivity implements Serializable {
private String cPcode;
Button help , sub;
EditText codetext;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent (this, Main2Activity.class);
sub = (Button) findViewById(R.id.submit);
help = (Button) findViewById(R.id.help);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = codetext.getText().toString();
MainActivity onew = new MainActivity();
onew.setcPcode(text);
intent.putExtra("stuff",onew);
startActivity(intent);
}
});}
and another problem is you why you are passing a new object of mainActivity so better option is create a separate POJO class and use it to store your data
Related
I did onClick at xml level. It was okay for a few times, but this one time causes error. I can't seem to find where the error is from. But it said that startActivity(intent); is where the error is.
public void callNextSignUpScreen2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp3.class);
startActivity(intent);
}
This is my full code
public class SignUp2 extends AppCompatActivity {
//Variable
ImageView back_icon;
Button next, login;
TextView title_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up2);
//Hooks
back_icon = findViewById(R.id.signup_back_btn);
next = findViewById(R.id.signup_next_next_btn);
login = findViewById(R.id.login_btn);
title_text = findViewById(R.id.signup_title_text);
}
public void callNextSignUpScreen2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp3.class);
startActivity(intent);
}
public void callBackSign2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
}
I'm trying to display the name inputted by the user on a basic form I created using Explicit intent on a second activity on a TextView but when I click the button the app crashes with the following "Unfortunately, formIntent has stopped" but my code has no errors. I got this to work with a Toast message on new activity but with a TextView. Any help would be appreciated.
Here's my First Activity `public class MainActivity extends AppCompatActivity {
Button submit;
TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.btnSubmit);
name = (TextView)findViewById(R.id.editTextname);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//from sending Activity
String userName = name.getText().toString();
String value = "Thank you " + userName + "your request is being processed" ;
Intent sendIntent = new Intent(v.getContext(), Main2Activity.class);
sendIntent.putExtra("userName", value);
//Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(sendIntent);
}
}
});
}
}
`
Here's My second Activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String getName = getIntent().getExtras().getString("userName");
final TextView inputMessage = (TextView)findViewById(R.id.display);
inputMessage.setText(getName);
}
}
Please change this line to your activity name
Intent sendIntent = new Intent(MainActivity.this, Main2Activity.class);
I´m new to app development and I´m trying to figure out how to send an user input like name, to a list view located in a diferent activity.
Thanks in advance.
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button mbuttonNext;
private EditText meditTextName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditTextName = (EditText) findViewById(R.id.editTextName);
mbuttonNext = (Button) findViewById(R.id.buttonNext);
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
startActivity(intent);
}
});
}
}
ListViewActivity:
public class ClientList extends AppCompatActivity {
private ListView listaClientes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
}
}
In the main activity you need to need intent.putExtra
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
intent.putExtra("VariableName", "Fred");
startActivity(intent);
}
});
Than in listView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
Bundle bundle = getIntent().getExtras(); //get the intent and data passed
//next check that bundle is not null
if (bundle != null) {
String name = bundle.getString("VariableName");
//try loging out the value
Log.i("Name", name);
}
}
I believe this will solve your problem
When starting the new activity:
Intent newIntent = new Intent(MainActivity.this, ClientList.class);
newIntent.putExtra("com.example.myapp.NAME", "someName");
startActivity(newIntent);
Then in ClientList:
Intent intent = getIntent();
String name = intent.getStringExtra("com.example.myapp.NAME");
Can then use name variable as source data for the ListView.
Usually you should create an Adapter that will "set" the items in the ListView. In order for you to send/transfer the user input to another activity you can use "putExtra()". You can do that the following way:
intent.putExtra("str1",item)
where str1 is the key to get your item in the activity you started. To do that you should do:
Bundle b = getIntent().getExtras();
Obj a = (TypeCast) b.get("str1");
After that you should read how to create an Adapter and set it in a ListView in order for your ListView to show the item.
i am new to android, i have created two activities where main activity consist one button to go to secondActivity in second activity where the user inputs name and the data is taken back to main activity using intents.The problem i am facing is i want to display the data in listview
MainActivity.java
public class MainActivity extends Activity {
Button addcontact;
ListView listview;
//TextView name;
ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
TextView name = (TextView) findViewById(R.id.txtname);
addcontact = (Button) findViewById(R.id.addbtn);
addcontact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 1);
savedata();
}
});
}
private void savedata() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
List<String> ListElements = new ArrayList<String>(Arrays.asList(fName));
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1, ListElements);
listview.setAdapter(adapter);
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText edname;
Button addbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
edname =(EditText) findViewById(R.id.edname);
addbtn = (Button) findViewById(R.id.savebtn);
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("fname", edname.getText().toString());
startActivity(intent);
}
});
}
}
I would suggest you don't recreate your first Activity. Instead of that approach, use startActivityForResult().
In short - Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For more chek this link.
Is there a way to open the system dialog settings->location & security->Install from SD card programmatically from my application?
You launch at least the security setting by the following code.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}