我是 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"));
}
}
错误(应用程序崩溃):
控制台输出:
[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] : 开始: 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:
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.