私は Android アプリと Java の両方を初めて使用し、単純なアプリを作成しています。エミュレータを開くと、「残念ながらアプリが停止しました」というエラーが表示されるまで、すべてが順調に進んでいます。ここに私の主な activity.java ファイルがあります:
package com.firstprogram;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout layout1;
EditText number1text;
EditText number2text;
Button calcButton;
TextView answerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1 = new LinearLayout (this);
number1text = new EditText (this);
number2text = new EditText (this);
calcButton = new Button (this);
answerText = new TextView (this);
layout1.setOrientation(LinearLayout.VERTICAL);
answerText.setText("0");
calcButton.setText("Press to Multiply");
calcButton.setOnClickListener((android.view.View.OnClickListener) multiply);
layout1.addView(number1text);
layout1.addView(number2text);
layout1.addView(calcButton);
layout1.addView(answerText);
setContentView(layout1);
}
private OnClickListener multiply = new OnClickListener() {
@Override
public void onClick (View v){
String firstString = number1text.getText().toString();
String secondString = number2text.getText().toString();
double firstNumber = Double.parseDouble(firstString);
double secondNumber = Double.parseDouble(secondString);
double result = firstNumber * secondNumber;
String resultString = String.valueOf(result);
answerText.setText(resultString);
}
};
}
ログキャットコード:
08-21 05:08:45.528: D/AndroidRuntime(809): Shutting down VM
08-21 05:08:45.528: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-21 05:08:45.587: E/AndroidRuntime(809): FATAL EXCEPTION: main
08-21 05:08:45.587: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstprogram/com.firstprogram.MainActivity}: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.os.Looper.loop(Looper.java:137)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 05:08:45.587: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 05:08:45.587: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 05:08:45.587: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 05:08:45.587: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 05:08:45.587: E/AndroidRuntime(809): at dalvik.system.NativeStart.main(Native Method)
08-21 05:08:45.587: E/AndroidRuntime(809): Caused by: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 05:08:45.587: E/AndroidRuntime(809): at com.firstprogram.MainActivity.onCreate(MainActivity.java:37)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.Activity.performCreate(Activity.java:5133)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 05:08:45.587: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 05:08:45.587: E/AndroidRuntime(809): ... 11 more
08-21 05:08:45.823: D/dalvikvm(809): GC_FOR_ALLOC freed 278K, 16% free 2739K/3240K, paused 61ms, total 94ms
08-21 05:10:27.568: D/AndroidRuntime(860): Shutting down VM
08-21 05:10:27.568: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-21 05:10:27.615: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-21 05:10:27.615: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstprogram/com.firstprogram.MainActivity}: java.lang.NullPointerException
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 05:10:27.615: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 05:10:27.615: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 05:10:27.615: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 05:10:27.615: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 05:10:27.615: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
08-21 05:10:27.615: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
08-21 05:10:27.615: E/AndroidRuntime(860): at android.view.ViewGroup.addViewInner(ViewGroup.java:3505)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.view.ViewGroup.addView(ViewGroup.java:3377)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.view.ViewGroup.addView(ViewGroup.java:3353)
08-21 05:10:27.615: E/AndroidRuntime(860): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:286)
08-21 05:10:27.615: E/AndroidRuntime(860): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:276)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.Activity.setContentView(Activity.java:1915)
08-21 05:10:27.615: E/AndroidRuntime(860): at com.firstprogram.MainActivity.onCreate(MainActivity.java:24)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.Activity.performCreate(Activity.java:5133)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 05:10:27.615: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 05:10:27.615: E/AndroidRuntime(860): ... 11 more
08-21 05:11:37.875: D/AndroidRuntime(907): Shutting down VM
08-21 05:11:37.875: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-21 05:11:38.155: E/AndroidRuntime(907): FATAL EXCEPTION: main
08-21 05:11:38.155: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstprogram/com.firstprogram.MainActivity}: java.lang.NullPointerException
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:137)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 05:11:38.155: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 05:11:38.155: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 05:11:38.155: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 05:11:38.155: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 05:11:38.155: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)
08-21 05:11:38.155: E/AndroidRuntime(907): Caused by: java.lang.NullPointerException
08-21 05:11:38.155: E/AndroidRuntime(907): at android.view.ViewGroup.addViewInner(ViewGroup.java:3505)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.view.ViewGroup.addView(ViewGroup.java:3377)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.view.ViewGroup.addView(ViewGroup.java:3353)
08-21 05:11:38.155: E/AndroidRuntime(907): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:286)
08-21 05:11:38.155: E/AndroidRuntime(907): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:276)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.Activity.setContentView(Activity.java:1915)
08-21 05:11:38.155: E/AndroidRuntime(907): at com.firstprogram.MainActivity.onCreate(MainActivity.java:23)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.Activity.performCreate(Activity.java:5133)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 05:11:38.155: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 05:11:38.155: E/AndroidRuntime(907): ... 11 more
08-21 05:53:02.416: D/AndroidRuntime(971): Shutting down VM
08-21 05:53:02.416: W/dalvikvm(971): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-21 05:53:02.469: E/AndroidRuntime(971): FATAL EXCEPTION: main
08-21 05:53:02.469: E/AndroidRuntime(971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstprogram/com.firstprogram.MainActivity}: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.os.Looper.loop(Looper.java:137)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 05:53:02.469: E/AndroidRuntime(971): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 05:53:02.469: E/AndroidRuntime(971): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 05:53:02.469: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 05:53:02.469: E/AndroidRuntime(971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 05:53:02.469: E/AndroidRuntime(971): at dalvik.system.NativeStart.main(Native Method)
08-21 05:53:02.469: E/AndroidRuntime(971): Caused by: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 05:53:02.469: E/AndroidRuntime(971): at com.firstprogram.MainActivity.onCreate(MainActivity.java:36)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.Activity.performCreate(Activity.java:5133)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 05:53:02.469: E/AndroidRuntime(971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 05:53:02.469: E/AndroidRuntime(971): ... 11 more
08-21 06:00:07.957: D/AndroidRuntime(1010): Shutting down VM
08-21 06:00:08.016: W/dalvikvm(1010): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-21 06:00:08.055: E/AndroidRuntime(1010): FATAL EXCEPTION: main
08-21 06:00:08.055: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstprogram/com.firstprogram.MainActivity}: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:137)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 06:00:08.055: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 06:00:08.055: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:525)
08-21 06:00:08.055: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 06:00:08.055: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 06:00:08.055: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method)
08-21 06:00:08.055: E/AndroidRuntime(1010): Caused by: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
08-21 06:00:08.055: E/AndroidRuntime(1010): at com.firstprogram.MainActivity.onCreate(MainActivity.java:36)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.Activity.performCreate(Activity.java:5133)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 06:00:08.055: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-21 06:00:08.055: E/AndroidRuntime(1010): ... 11 more