I have two activities; Home is my first activity and Settings is my second activity.
The Settings activity is called from the menu of the Home activity, and returns data to home activity through intent.
But first time when I run the android application, the intent will be empty because the second activity is not yet called, so I get exception there (Java null pointer exception).
Can anybody help me out to handle this solution?
EDIT: Code
First Activity:
public class LoginActivity extends Activity {
public static final int SETTINGS_ID = 1;
Intent intn;
EditText edt1;
EditText edt2;
String user, pass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intn= new Intent(this,SettingsActivity.class);
}
private class HttpConnectionRequest extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void...params) {
// TODO Auto-generated method stub
//Code not included
}
return null;
}
}
public void login(View V){
edt1 = (EditText) this.findViewById(R.id.editText1);
edt2 = (EditText) this.findViewById(R.id.editText2);
user = edt1.getText().toString();
pass = edt2.getText().toString();
System.out.println("Username-->" + user);
System.out.println("Password-->" + pass);
String url="";
url = getIntent().getExtras().getString("serverurl");
System.out.println("url----->"+url);
if(((null==user) || ("".equals(user))) && ((null==pass) || ("".equals(pass)) )){
Toast.makeText(getApplicationContext(), "Please provide username and password", Toast.LENGTH_SHORT).show();
}
else if((null==user) || ("".equals(user))){
Toast.makeText(getApplicationContext(), "Please provide username", Toast.LENGTH_SHORT).show();
}
else if ((null==pass) || ("".equals(pass))){
Toast.makeText(getApplicationContext(), "Please provide password", Toast.LENGTH_SHORT).show();
}
if ((null==url) || ("".equals(url))) {
Toast.makeText(getApplicationContext(), "Please provide the Server url Settings->Server URL->Save", Toast.LENGTH_SHORT).show();
}
HttpConnectionRequest conn= new HttpConnectionRequest();
conn.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, SETTINGS_ID, 0, "Settings");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SETTINGS_ID:
//this.finish();
this.startActivity(intn);
System.out.println("This Invoked . . .");
break;
}
return false;
}
}
Second activity
public class SettingsActivity extends Activity {
EditText edt1;
Intent intn;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
intn = new Intent (this,LoginActivity.class);
}
public void save(View v){
edt1= (EditText) this.findViewById(R.id.serverurl);
String url = edt1.getText().toString();
intn.putExtra("serverurl",url);
startActivity(intn);
}
}
This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Use startActivityForResult in your calling activity to start the activity: ActivityCompat. startActivityForResult(this, new Intent(this, MyActivity. class), 0, null);
You can do a check for the resultCode
as below.
http://developer.android.com/guide/topics/fundamentals/activities.html#StartingAnActivityForResult
Also, can you identify where exactly you get the exception? (post some code maybe)
EDIT:
Ok, I still donT know where you are calling your login()
method but you can do this check to avoid null exceptions in the line you mentioned:
if( getIntent().getExtras() != null)
{
//do here
}
You can try:
if(intent.getExtras() == null) {
//Do first time stuff here
} else {
//Do stuff with intent data here
}
If you are passing back different intent values every time, you can check if your Bundle object (returned with intent.getExtras()) has certain fields set using this:
Bundle extras = intent.getExtras()
if(extras.containsKey("keytocheck")) {
//Do stuff because extra has been added
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With