从意图获得额外的时候空指针异常?

时间:2013-01-28 18:31:09

标签: java android android-intent

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


}

第一堂课在上面,第二堂课在

之下
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;
    }

}

在mainActivity中,每按一次按钮abButton,它会将count变量增加1.它还会创建一个新的intent并通过extra将该变量发送给该intent。 在第二个活动中,从intent获取数据的“values”变量给了我一个空指针异常。为什么呢?

2 个答案:

答案 0 :(得分:4)

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

更改为

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

在您开始第二项活动后,您正在设置额外费用

答案 1 :(得分:2)

只需改变这两个代码行,

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

您的 SecondActivity Intent在设置额外计数之前就已开始。