- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一个初学者,我需要一些建议。当我尝试在 Android 模拟器中运行以下 Android 应用程序时,当我尝试运行它时,它显示“不幸的是,再次已关闭”。 我不确定为什么它不起作用。 有人可以给我建
我正在尝试在 Android Studio 中制作一个应用程序,它要求您将文件下载到用户设备上的 sdcard/Download/文件夹中。问题是,当我输入要下载的 URL 并单击“下载”时,应用程序
自上次更新我的 Android Studio 和我的 SDK 包以来,每当我启动模拟器时都会收到此错误。请检查下面的屏幕截图。 我的 logcat 将这些报告为错误语句: 06-11 05:18:22
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
这个问题在这里已经有了答案: Unfortunately MyApp has stopped. How can I solve this? (22 个回答) 3年前关闭。 我用我在 github 上找
在我的应用程序中单击图像按钮时,我总是收到消息:“不幸的是,myapp已停止”。问题是什么??? 这是我的代码。单击图像按钮将调用showImageDialog方法: public class Rem
我是个新人,正在从新的波士顿学习编码。 MainActivity.java: package com.tipsoftech.fastfart; import android.app.Activity;
我是 Android 应用程序和 Java 的新手,一直在制作一个简单的应用程序,一切都很顺利,直到我打开模拟器并出现以下错误“不幸的是,您的应用程序已停止”。这是我的主要 Activity.java
我是 Android 应用程序开发新手,我尝试编写我的第一个简单应用程序来尝试在 Activity 之间发送数据。代码已编译,我尝试在虚拟设备上启动它,但正如标题所示,它停止了。 这是 logcat
这是我的第一个应用程序。我所需要的只是五个按钮,其中两个用于调用某些电话号码(到目前为止只创建了一个),三个按钮用于将用户带到某个 URL。我没有收到任何错误或警告,也没有任何有关如何导航 LogCa
运行应用程序时收到以下错误: 02-09 18:01:19.582: D/AndroidRuntime(1306): Shutting down VM 02-09 18:01:19.582: W/da
我是 Android 编程新手。我正在遵循 youtube 上的 Android 教程指南。这是我们的论文。 这是主 Activity package com.example.abrico.viola
我正在尝试为 android 编写一个基本的绘图 Android 应用程序并在 Genymotion 上测试它,但我无法单击应用程序图标(它显示“不幸的是 [app] 已停止。”) 我不确定这三个私有
你好,我在运行应用程序时遇到问题,当它运行不正常时,它会打开,但是当我按下按钮时,告诉我应用程序已停止并且无法运行:“不幸的是,应用程序已停止”。这是我的简单代码,我希望我能尽快得到答案......谢
我的应用程序只工作一次,在第二次运行时(和其他...)我得到“不幸的是,我的应用程序已停止”。我已经尝试了很多,但对我没有任何影响。这是代码: 主要 Activity : 包 com.example.
你好,我是新编程,这是我的第一个应用程序,我试图重新创建一个我找到教程的应用程序。是一个小费计算器,应用程序打开没有问题,但当我尝试使用它时,它停止工作并关闭。 XML代码是 package com.
当我使用模拟器运行我的 Android 应用程序时,它运行良好,在我的手机上也是如此,但在另一部手机上,尝试运行该应用程序时,我总是得到“不幸的是 [...] 已停止”错误。我如何找出这个错误的原因(
我想将位图实现到surfaceview,并尝试在单击时通过按钮编辑图像。但有一个问题。当我在模拟器上运行它时,一切都是正确的,直到按下按钮。 “不幸的是,......停止......”出现在屏幕上,程
我的应用程序运行正常! 当运行android应用程序编译无误,但在显示时返回消息。 Unfortunately, application has stopped 当我安装并链接这两个包时发生了 rea
这是一个食物食谱应用程序,我只是在学习,所以如果你能帮我纠正我的错误并解释,我将非常感激。提前致谢 主要 Activity package rice.recipe; import android.ap
我是一名优秀的程序员,十分优秀!