私はAndroidを初めて使用し、インテントによってアクティビティから別のアクティビティにいくつかの変数を渡す方法を学習しようとしています。問題は、質問への回答が終了した後にアプリを実行すると、AskQusetionsアクティビティの結果(yesとnoのカウント)を持つSummaryアクティビティに移動することです。
これが私の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"));
}
}
エラー(アプリがクラッシュする):
consol出力:
[2012-10-16 01:28:04-QuizApp] Androidローンチ![2012-10-1601:28:04-QuizApp]adbは正常に実行されています。[2012-10-16 01:28:04-QuizApp]com.example.quizapp.MainActivityアクティビティの起動を実行しています[2012-10-1601:28:04-QuizApp]自動ターゲットモード:既存のエミュレーター「エミュレーター-5554」を使用互換性のあるAVD「firstDevice」を実行しています[2012-10-1601:28:05-QuizApp]ポート8621で「com.example.quizapp」にデバッガーを接続しようとしています[2012-10-1601:28:07-QuizApp]ActivityManager :開始:インテント{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.quizapp / .MainActivity} [2012-10-16 01:28:08-QuizApp]アプリケーションはすでにデプロイされています。再インストールする必要はありません。[2012-10-1601:28:08-QuizApp]デバイスエミュレーターでアクティビティcom.example.quizapp.MainActivityを開始します-5554[2012-10-1601:28:
Logcatエラー(日食を再開した後):
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.