gpt4 book ai didi

java - "Unfortunately your app has stopped"模拟器错误

转载 作者:行者123 更新时间:2023-12-01 14:12:59 26 4
gpt4 key购买 nike

我是 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

最佳答案

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() {
....
}

关于java - "Unfortunately your app has stopped"模拟器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com