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);
}
}
You can do a check for the
resultCodeas 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: