通过Intent将整数值传递给另一个活动。不行

时间:2012-10-16 07:31:39

标签: android android-intent

我是Android的新手,并试图学习如何通过Intents将一些变量从活动传递到另一个活动。问题是当我在完成回答我的问题后运行应用程序时,它将带我进入具有AskQusetions活动结果的摘要活动(是和否的计数)

这是我的AskQuestion活动:

    package com.example.quizapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class AskQuestions extends Activity {

    private String[] messages;
    private int yesCount;
    private int noCount;
    private int messageNum;
    public final static String EXTRA_MESSAGE = "com.example.quizapp.MESSAGE";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_questions);

    }

    @Override
    public void onStart() {
        super.onStart();
        messages = getResources().getStringArray(R.array.lblask_questions);
        messageNum = 0;
        setYesCount(0);
        setNoCount(0);

        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public void onYes(View view) {
        Log.i("Quiz App", "viewing next question");

        setYesCount(getYesCount() + 1); // count yes
        messageNum++;
        if (messageNum >= messages.length) { // if the questions finished got to summary
            // go to summary
            Intent summaryIntent = new Intent(this, Summary.class);
            //int messageYes = getYesCount();
            summaryIntent.putExtra("value", getYesCount());
            startActivity(summaryIntent);
        }

        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public void onNo(View view) {
        Log.i("Quiz App", "viewing next question");

        setNoCount(getNoCount() + 1); // count not
        messageNum++;
        if (messageNum >= messages.length) { // if the questions finished got to summary
            // go to summary
            Intent summaryIntent = new Intent(this, Summary.class);
            summaryIntent.putExtra("value", getNoCount()); 
            startActivity(summaryIntent);
        }



        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public int getYesCount() {
        return yesCount;
    }

    public void setYesCount(int yesCount) {
        this.yesCount = yesCount;
    }

    public int getNoCount() {
        return noCount;
    }

    public void setNoCount(int noCount) {
        this.noCount = noCount;
    }

}

以下是我的摘要活动:

package com.example.quizapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


/**
 * @author ***
 *
 */
public class Summary extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Bundle extras = getIntent().getExtras();
        //String value = extras.getString(AskQuestions.EXTRA_MESSAGE);


        setContentView(R.layout.summary);

    }
    @Override
    public void onStart() {
        super.onStart();

        //String value = "HELLO";

        TextView lblYes = (TextView)findViewById(R.id.yes_result);
        lblYes.setText(getIntent().getExtras().getString("value"));

        TextView lblNo = (TextView)findViewById(R.id.no_result);
        lblNo.setText(getIntent().getExtras().getString("value"));


    }


}

错误(app崩溃):

ScreenShot App Crash

consol输出:

[2012-10-16 01:28:04 - QuizApp] Android发布! [2012-10-16 01:28:04 - QuizApp] adb正常运行。 [2012-10-16 01:28:04 - QuizApp]执行com.example.quizapp.MainActivity活动启动 [2012-10-16 01:28:04 - QuizApp]自动目标模式:使用现有的仿真器'emulator-5554'运行兼容的AVD'firstDevice' [2012-10-16 01:28:05 - QuizApp]尝试在端口8621上将调试器连接到'com.example.quizapp' [2012-10-16 01:28:07 - QuizApp] ActivityManager:开始:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.quizapp / .MainActivity } [2012-10-16 01:28:08 - QuizApp]已部署应用程序。无需重新安装。 [2012-10-16 01:28:08 - QuizApp]在设备模拟器-5554上启动com.example.quizapp.MainActivity活动 [2012-10-16 01:28:10 - QuizApp] ActivityManager:开始:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.quizapp / .MainActivity } [2012-10-16 01:28:10 - QuizApp]尝试在端口8621上将调试器连接到'com.example.quizapp' [2012-10-16 01:28:12 - QuizApp]试图在端口8621上将调试器连接到'com.example.quizapp'

Logcat错误(重新启动eclipse后):

10-16 02:17:34.469: D/AndroidRuntime(1496): Shutting down VM
10-16 02:17:34.469: W/dalvikvm(1496): threadid=1: thread exiting with uncaught exception (group=0xb3f68288)
10-16 02:17:34.479: E/AndroidRuntime(1496): FATAL EXCEPTION: main
10-16 02:17:34.479: E/AndroidRuntime(1496): java.lang.IllegalStateException: Could not execute method of the activity
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$1.onClick(View.java:3591)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View.performClick(View.java:4084)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$PerformClick.run(View.java:16966)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Handler.handleCallback(Handler.java:615)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Looper.loop(Looper.java:137)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at dalvik.system.NativeStart.main(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496): Caused by: java.lang.reflect.InvocationTargetException
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$1.onClick(View.java:3586)
10-16 02:17:34.479: E/AndroidRuntime(1496):     ... 11 more
10-16 02:17:34.479: E/AndroidRuntime(1496): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.example.quizapp.AskQuestions.onYes(AskQuestions.java:56)
10-16 02:17:34.479: E/AndroidRuntime(1496):     ... 14 more
10-16 02:17:38.499: E/Trace(1604): error opening trace file: No such file or directory (2)
10-16 02:17:39.020: D/gralloc_goldfish(1604): Emulator without GPU emulation detected.

4 个答案:

答案 0 :(得分:1)

putExtra()的第一个参数是一个String,它包含应用程序的包名和值的名称,例如“com.example.quizapp.yesValues”或“com.example.quizapp.noValues”

然后通过

检索值
getIntent().getExtras().getInt("com.example.quizapp.yesValues", -1);
getIntent.getExtras().getInt("com.example.quizapp.noValues", -1);

请注意getInt()中的第二个参数是默认值

您的第二个错误是由IndexOutOfBoundsException引起的。您正尝试访问数组中不存在的索引:

lblQuestion.setText(messages[messageNum]);

在你的onYes()和onNo()方法中。请注意,在它上方检查messageNum> = messages.length,然后立即通过它解决数组。在支票内执行此操作

if(messageNum < messages.length)
祝你好运,编码愉快!

答案 1 :(得分:1)

我相信你因为这行代码而得到了Exception:

lblQuestion.setText(messages[messageNum]);
onYes(View view);方法

中的

messageNum value = 5;

其中,消息数组中的总项数为5

message.length() = 5;

因此ArrayIndexOutOfBounds Exception

答案 2 :(得分:0)

你必须使用:

Intent summaryIntent = new Intent(this, Summary.class);
Bundle b=new Bundle();
b.putInt("Integer", IntValue);
summaryIntent.putExtras(b);
startActivity(summaryIntent);

对于接收整数值,请使用:

Bundle b = getIntent().getExtras();
mInt= b.getInt("Integer");

此代码对我有用。

由于

答案 3 :(得分:0)

您正在尝试在TextView中设置int值。我认为这可能有所帮助:

    TextView lblYes = (TextView)findViewById(R.id.yes_result);
    lblYes.setText(Integer.toString(getIntent().getExtras().getInt("value")));

你应该把额外的东西放在这样的包中:

    Bundle bundleObj = new Bundle();     
    bundleObj.putInt("value", getYesCount()); 
    summaryIntent.putExtras(bundleObj);