0
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button abButton = (Button) findViewById(R.id.button1);
        final TextView changelingtext = (TextView) findViewById(R.id.changeling);
         abButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Toast.makeText(getBaseContext(), "Buttons are working baby", Toast.LENGTH_LONG).show();
                 count++;
                String a = Integer.toString(count);
                 changelingtext.setText(a); 
                 gotonextpage(v);
             }
         });
    }
    public void gotonextpage(View view){
        Intent intent = new Intent(this, SecondpageActivity.class);
        startActivity(intent);
        intent.putExtra("count", count);
        //finish(); if you want to end this page

    }
    @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;
    }


}

First class is above, second class is below

package com.example.collegematch;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SecondpageActivity extends Activity {
    int values;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondpage);
        Intent intent = getIntent();
        values = intent.getExtras().getInt("count");
        Button exitButton = (Button) findViewById(R.id.exit);
        Button textbutton = (Button) findViewById(R.id.coolbutton);
        TextView texty = (TextView) findViewById(R.id.cooltext);
        textbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), Integer.toString(values), Toast.LENGTH_LONG).show();
                System.out.println(values);
            }
        });
        exitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Seeya", Toast.LENGTH_LONG).show();

                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.secondpage, menu);
        return true;
    }

}

In the mainActivity, everytime button abButton is pressed, it increases the count variable by 1. It also creates a new intent and sends that variable via extra to that intent. In the second activity, the "values" variable getting the data from the intent is giving me a null pointer exception. Why?

1
  • please post the logcat of the exception Commented Jan 28, 2013 at 18:32

2 Answers 2

4
 Intent intent = new Intent(this, SecondpageActivity.class);
 startActivity(intent);
 intent.putExtra("count", count);

Change to

 Intent intent = new Intent(this, SecondpageActivity.class);
 intent.putExtra("count", count);
 startActivity(intent);

You are setting the extra after you've already started the 2nd activity

Sign up to request clarification or add additional context in comments.

Comments

2

just alter these two code lines,

Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);

Your SecondActivity Intent start before setting the extra count to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.