我是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);
}
};
}
Log Cat代码:
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
答案 0 :(得分:1)
logcat中有趣的一行是:
Caused by: java.lang.ClassCastException: com.firstprogram.MainActivity$1 cannot be cast to android.view.View$OnClickListener
Android框架中有多个OnClickListener
定义,似乎您没有使用正确的定义。
如果您想使用View.OnClickListener
,那么您应该使用:
private View.OnClickListener multiply = new View.OnClickListener() {
....
}
答案 1 :(得分:0)
您的班级OnClickListener
不会继承android.view.View.OnClickListener
您应该在类中更好地实现android.view.View.OnClickListener
,并覆盖类似的方法:
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Do your stuff
calcButton.setOnClickListener(this);
}
@Override
public void onClick (View v){
//handle the event
}
答案 2 :(得分:0)
正如Vimal所说,以这种方式生成GUI并不是一个好主意。但是,试试这个:
calcButton.setOnClickListener(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);
}
});