Reputation: 33
I am Getting a null pointer exception when I click an options button that changes activities here is the buttons code
options.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent i = new Intent(Main_Timer_Activity.this,Options_Activity.class);
startActivity(i);
finish();
}
});
And I Only have an initialise method on the options activity
private void initialise()
{
minutes = (EditText)findViewById(R.id.edtMin);
seconds = (EditText)findViewById(R.id.edtSec);
coffeeS = (Spinner)findViewById(R.id.spinCOF);
save = (Button)findViewById(R.id.btnSave);
ArrayAdapter<String>adapter = new ArrayAdapter<String>
(Options_Activity.this,android.R.layout.simple_spinner_item,coffee);
coffeeS.setAdapter(adapter);
}
Here is the options Activity on create
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options_);
initialise();
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Options_Activity.this,Main_Timer_Activity.class);
startActivity(i);
finish();
}
});
Upvotes: 0
Views: 76
Reputation: 1039
Null pointer exceptions are caused because of the misprint ids.In your case,you forgot to initialize the back button. Initialize your back button.
Upvotes: 0
Reputation: 4522
As you have not posted your logcat error, so not so much clear the exact reason of it but we can see that you have not initialized back
button in initialise()
function. do that as
private void initialise()
{
minutes = (EditText)findViewById(R.id.edtMin);
seconds = (EditText)findViewById(R.id.edtSec);
coffeeS = (Spinner)findViewById(R.id.spinCOF);
save = (Button)findViewById(R.id.btnSave);
back=(Button)findViewById(R.id.back_button_id);
ArrayAdapter<String>adapter = new ArrayAdapter<String>
(Options_Activity.this,android.R.layout.simple_spinner_item,coffee);
coffeeS.setAdapter(adapter);
}
and make an entry of this activity in manifest.xml
file also i.e.
<activity android:name="Options_Activity"/>
Upvotes: 0
Reputation: 21531
Initialize your button like:
Button back = (Button) findViewById(R.id.getIdOfYourButton);
then perform click action like:
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(context, "clicked on backimage ", Toast.LENGTH_SHORT);
toast.show();
//more code
}
});
Upvotes: 0
Reputation: 3585
Have you added permission in manifest file?
<activity
android:name=".Main_Timer_Activity"
/>
Upvotes: 0