-1

我是 Android 新手,我正在尝试使用 Intent 将整数值从一个 .java 类传递给另一个。该整数在第一个 .java 文件中声明,并在 IF 语句中使用以产生分数;

public class Diabetes_Question_1 extends Activity {

public int Total = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diabetes_question_1);

    Button btnBack = (Button) findViewById(R.id.btnBack1);
    btnBack.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Diabetes_Question_1.this, Diabetes_Question_2.class));

        }
    });

    final RadioButton rb1 = (RadioButton) findViewById(R.id.btnDQ1Radio1);
    final RadioButton rb2 = (RadioButton) findViewById(R.id.btnDQ1Radio2);
    final RadioButton rb3 = (RadioButton) findViewById(R.id.btnDQ1Radio3);
    Button btnNext = (Button) findViewById(R.id.btnNext1);
    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(rb1.isChecked()==true) {
                Total = 1;
            } else if (rb2.isChecked()==true) {
                Total = 2;
            } else if (rb3.isChecked()==true) {
                Total = 3;
            } else {
                Total = 4;
            }

            Toast.makeText(Diabetes_Question_1.this, String.valueOf(Total), Toast.LENGTH_SHORT).show();

            Intent i = new Intent(Diabetes_Question_1.this, Diabetes_Question_3.class);
            i.putExtra("totalScore", Total);
            startActivity(i);
        }
    });
}
}

一旦产生了这个分数,我想将它传递给另一个 .java 类,以便可以根据下一个 IF 语句添加分数;

public class Diabetes_Question_3 extends Activity {

Bundle extras = getIntent().getExtras();
int Total3 = extras.getInt("totalScore");

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diabetes_question_3);

    Button btnBack = (Button) findViewById(R.id.btnBack2);
    btnBack.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Diabetes_Question_3.this, Diabetes_Question_1.class));

        }
    });

    final RadioButton rb1 = (RadioButton) findViewById(R.id.btnDQ3Radio1);
    final RadioButton rb2 = (RadioButton) findViewById(R.id.btnDQ3Radio2);
    final RadioButton rb3 = (RadioButton) findViewById(R.id.btnDQ3Radio3);
    Button btnNext = (Button) findViewById(R.id.btnNext3);
    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Diabetes_Question_3.this, Diabetes_Question_4.class));

            if(rb1.isChecked()==true) {
                Total3 = + 1;
            } else if (rb2.isChecked()==true) {
                Total3 = + 1;
            } else if (rb3.isChecked()==true) {
                Total3 = + 1;
            } else {
                Total3 = + 1;
            }

            Toast.makeText(Diabetes_Question_3.this, String.valueOf(Total3), Toast.LENGTH_SHORT).show();

        }
    });
}
}

我知道 IF 语句有效,因为我有一个祝酒词来证明这一点。然而,转移到下一个 .java 类是问题所在,我正在努力改进代码。

我给出以下错误信息;

致命异常:主要 java.lang.RuntimeException:无法实例化活动 ComponentInfo {PACKAGE NAME} java.lang.NullPointerExcpetion

任何帮助将不胜感激。

4

1 回答 1

1

放这个。

Bundle extras = getIntent().getExtras();
int Total3 = extras.getInt("totalScore");

在 Diabetes_Question_3 活动的 onCreate 方法中。

您应该遵循 Java 的命名约定。

int total3 = 0; //Instance variable.

Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diabetes_question_3);

    .........
    Bundle extras = getIntent().getExtras();
    total3 = extras.getInt("totalScore");
于 2013-03-20T17:06:05.260 回答