user2694368
user2694368

Reputation: 33

Null Pointer Exception when changing activities

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

Answers (7)

Pihu
Pihu

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

Mukesh Kumar Singh
Mukesh Kumar Singh

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

Shailendra Madda
Shailendra Madda

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

silvia_aut
silvia_aut

Reputation: 1493

Initialize your back Button. Should solve the problem

Upvotes: 1

Milan Delvadia
Milan Delvadia

Reputation: 90

check your coffee array in Array adapter.... Is it null???

Upvotes: 0

d3m0li5h3r
d3m0li5h3r

Reputation: 1967

the coffee variable in the initialise() method may be null..

Upvotes: 0

Looking Forward
Looking Forward

Reputation: 3585

Have you added permission in manifest file?

 <activity
            android:name=".Main_Timer_Activity"
             />

Upvotes: 0

Related Questions