Reputation: 3
Hi I am beginner in the android I am doing small tasks like creating activities,buttons,check box etc.The code will work fine and I will get desired output.If i try to add new which may contain error I will get unfortunately app stopped then again if i undo all the changes what I did and run the code again will get same error.
what is the problem and what I have to do? This is the code after undoing changes is there any problem?This is the main activity.Please any one help me.
CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText text= (EditText) findViewById (R.id.editText1);
EditText text1= (EditText) findViewById (R.id.editText2);
SharedPreferences sp=getSharedPreferences(MY_FILE,Context.MODE_PRIVATE);
String textview1= text.getText().toString();
String password1= text1.getText().toString();
String oldname=sp.getString("name",name);
String oldpassword=sp.getString("password","");
if(!textview1.equals(null) && !password1.equals(null))
{
if(textview1.equals(oldname)&& password1.equals(oldpassword))
{
Intent i=new Intent(MainActivity.this,MenuScreen.class);
startActivity(i);
}
else{
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("error msg");
alertDialog.setMessage("You should register before");
alertDialog.show();
}
}
}
});
button2= (Button)findViewById (R.id.button2);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent j=new Intent(MainActivity.this,Registration.class);
startActivity(j);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Upvotes: 0
Views: 99
Reputation: 3633
After calling setContentView() only your layout will be loaded.So put below code
CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1);
in onCreate() after setContentView().
Upvotes: 0
Reputation: 14590
This Line Causes you error..
CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1);
The above line not out side the onCreate()
method..place it inside onCreate()
because after setContentView()
only you will get the View Object.
Upvotes: 2
Reputation: 2395
CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1);
put into onCreate()
Upvotes: 1