java - 通过 Intent 将整数传递给 Activity - Java (Android)

标签 java android android-intent bundle

Results.java 中所有注释掉的代码都是我尝试过但没有成功的代码。我无法按 Intent 将整数传递到另一个 Activity 。

以下方法位于我的 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

最佳答案

您正尝试在 Results 的 onCreate 方法之前从 QuestionView 获取 Intent 。将用于接收结果的代码放置在 onCreate 中,将您的 Result Activity 更改为:

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 class用于在应用程序组件(如 Activity )之间共享数据

关于java - 通过 Intent 将整数传递给 Activity - Java (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13707994/

相关文章:

java - Android - Stream 无法正确读取数组

android - 替换 android Intent 选择器对话框

java - onActivityResult()之后动态添加的imageView消失

java - 需要帮助修复我的选择排序

java - 从多个 REST 端点组装 Swagger 文档的最佳方式

java - --i 和 i-- 的行为与++i 和 i++ 相同吗?

android - Firebase - 检索后单击按钮编辑/更新数据库

java - 存储图像,然后在 Android 上的 ImageView 中显示它

android - 如何从 IntentService 返回一个对象?

java - 为什么将函数参数声明为 final?