0

Results.java 中所有被注释掉的代码都是我尝试过的所有事情,但都没有成功。我无法通过意图将整数传递给另一个活动。

下面的方法在我的 QuestionView.java 类中。

public void endQuiz() {
    Intent intent = new Intent(QuestionView.this, Results.class);
    intent.putExtra("correctAnswers", correctAnswers);
    intent.putExtra("wrongAnswers", wrongAnswers);
    startActivity(intent);
}

结果.java

public class Results extends Activity {

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

//  int cAswers = this.getIntent().getExtras("correctAnswers");
    int cAnswers = getIntent().getIntExtra("correctAnswers", -1);
    int wAnswers = getIntent().getIntExtra("wrongAnswers", -1);

/*  Bundle b = this.getIntent().getExtras();
    int cAnswers = b.getInt("correctAnswers");
    int wAnswers = b.getInt("wrongAnswers");*/

//  int cAnswer = getIntent().getIntExtra("correctAnswer");


//  if(getIntent().getIntExtra("correctAnswers") != null) {
//      int cAnswers = b.getInt("correctAnswers");
//  }

//  ArrayList<Question> questions = extras.getSparseParcelableArray("queries");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);



/*      Bundle extras = getIntent().getExtras();
        int cAnswers = extras.getInt("correctAnswers");
        int wAnswers = extras.getInt("wrongAnswers");*/


        Button homeBtn = (Button)findViewById(R.id.homeBtn);
        Button highscoresBtn = (Button)findViewById(R.id.highscoresBtn);

        homeBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent1 = new Intent(Results.this, MainMenu.class);
                startActivity(intent1);
            }
        });

        highscoresBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent2 = new Intent(Results.this, Highscores.class);
                startActivity(intent2);
            }
        });

        showResults();
    }

    public void showResults() {

        ArrayList<TextView> tList = new ArrayList<TextView>(10);

        TextView header = (TextView)findViewById(R.id.header);
        TextView q1 = (TextView)findViewById(R.id.q1);
        TextView q2 = (TextView)findViewById(R.id.q2);
        TextView q3 = (TextView)findViewById(R.id.q3);
        TextView q4 = (TextView)findViewById(R.id.q4);
        TextView q5 = (TextView)findViewById(R.id.q5);
        TextView q6 = (TextView)findViewById(R.id.q6);
        TextView q7 = (TextView)findViewById(R.id.q7);
        TextView q8 = (TextView)findViewById(R.id.q8);
        TextView q9 = (TextView)findViewById(R.id.q9);
        TextView q10 = (TextView)findViewById(R.id.q10);

        tList.add(q1);
        tList.add(q2);
        tList.add(q3);
        tList.add(q4);
        tList.add(q5);
        tList.add(q6);
        tList.add(q7);
        tList.add(q8);
        tList.add(q9);
        tList.add(q10);

        tList.get(0).setText(queryList.get(0).getQuery());

        if(queryList.get(0).getCorrectness() == true) {
            tList.get(1).setText("Changed to true");
        } else {
            tList.get(1).setText("DID NOT Change to true");
        }

        tList.get(2).setText(Integer.toString(cAnswers));
        tList.get(3).setText(Integer.toString(wAnswers));

/*      for(int i = 0; i < 10; i++) {
            tList.get(i).setText(queryList.get(i).getQuery());
            if(queryList.get(i).getCorrectness() == true) {
                tList.get(i).setTextColor(Color.GREEN);
            } else {
                tList.get(i).setTextColor(Color.RED);
            }
        }*/
    }
}

日志猫

12-04 11:52:33.342: E/AndroidRuntime(1128): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.os.Looper.loop(Looper.java:137)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at dalvik.system.NativeStart.main(Native Method)
12-04 11:52:33.342: E/AndroidRuntime(1128): Caused by: java.lang.NullPointerException
12-04 11:52:33.342: E/AndroidRuntime(1128):     at com.example.test.Results.<init>(Results.java:21)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at java.lang.Class.newInstanceImpl(Native Method)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at java.lang.Class.newInstance(Class.java:1319)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
12-04 11:52:33.342: E/AndroidRuntime(1128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
12-04 11:52:33.342: E/AndroidRuntime(1128):     ... 11 more
4

1 回答 1

3

您正在尝试在Results的onCreate方法之前从QuestionView获取意图。将您的代码用于在onCreate中接收结果将您的结果活动更改为:

public class Results extends Activity {
    int cAnswers=0;
    int wAnswers=0;
    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);
        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
         wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
       //your code here...

注意:我已经告诉过你不要创建一个 Activity 实例来访问它的方法或字段。使用IntentApplication 类在应用程序组件(如活动)之间共享数据

于 2012-12-04T17:00:50.040 回答