不幸的是,“应用程序”已经停止了 Android Studio 的问题

Posted

技术标签:

【中文标题】不幸的是,“应用程序”已经停止了 Android Studio 的问题【英文标题】:Unfortunately "app" has stopped issue with Android Studio 【发布时间】:2016-12-09 21:10:40 【问题描述】:

社区

我在让此应用程序在 AVD 中运行时遇到问题。当我运行应用程序时,它开始加载,然后出现错误消息“不幸的是,MyMedicare 已停止”。我对 Google、Stack 和 Youtube 的研究并没有缓解任何问题。有人有意见吗?

我的主要活动:

    package com.example.user.mymedicare;

import com.example.user.mymedicare.R;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.*;

public class MainActivity extends AppCompatActivity 

    public String result;

    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.MainLayout);

        Button btnCalc = (Button) findViewById(R.id.btnCalc);
        btnCalc.setOnClickListener(new View.OnClickListener() 

            public void onClick(View MainLayout) 
                EditText etxtWeight = (EditText)
                        findViewById(R.id.etxtWeight);

                String strW = etxtWeight.getText().toString();
                double weight = Double.parseDouble(strW);

                EditText etxtHeight = (EditText)
                        findViewById(R.id.etxtHeight);

                String strH = etxtHeight.getText().toString();
                double height = Double.parseDouble(strH);

                double h = height * 0.01;
                double bmi = weight / (h*h);

                if (bmi <= 18.5) 
                    result = "You are Underweight!";
                
                else if ((bmi > 18.5) && (bmi <= 25.9)) 
                    result = "You are Healthy";
                
                else 
                    result = "You are Overweight!";
                

                String strBmi = Double.toString(Math.round(bmi));
                TextView txtResults = (TextView)
                        findViewById(R.id.txtResults);
                txtResults.setText("Your BMI is " + strBmi + " " + result);
            
        );
    

我的毕业生:

    apply plugin: 'com.android.application'

android 
    compileSdkVersion 24
    buildToolsVersion '24.0.1'

    defaultConfig 
        applicationId "com.example.user.mymedicare"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


dependencies 
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.0'
    compile 'com.android.support:design:24.1.0'

我的清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.mymedicare">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LogCat:

08-06 16:09:05.552 3502-3502/com.example.user.mymedicare I/art: Not late-enabling -Xcheck:jni (already on)
08-06 16:09:06.392 3502-3502/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:09:10.706 3502-3502/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:09:10.768 3502-3502/com.example.user.mymedicare W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-06 16:09:10.849 3502-3502/com.example.user.mymedicare D/AndroidRuntime: Shutting down VM


                                                                           --------- beginning of crash
08-06 16:09:10.849 3502-3502/com.example.user.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.user.mymedicare, PID: 3502
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.user.mymedicare/com.example.user.mymedicare.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.user.mymedicare.MainActivity.onCreate(MainActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-06 16:26:38.175 8171-8171/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:26:39.356 8171-8171/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:26:39.402 8171-8171/com.example.user.mymedicare W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-06 16:26:39.451 8171-8171/com.example.user.mymedicare D/AndroidRuntime: Shutting down VM
08-06 16:26:39.451 8171-8171/com.example.user.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.user.mymedicare, PID: 8171
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.user.mymedicare/com.example.user.mymedicare.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.user.mymedicare.MainActivity.onCreate(MainActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-06 16:26:42.782 8171-8171/com.example.user.mymedicare I/Process: Sending signal. PID: 8171 SIG: 9
08-06 16:26:45.505 8273-8273/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:26:46.583 8273-8273/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:26:46.630 8273-8273/com.example.user.mymedicare W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-06 16:26:46.681 8273-8273/com.example.user.mymedicare D/AndroidRuntime: Shutting down VM
08-06 16:26:46.681 8273-8273/com.example.user.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.user.mymedicare, PID: 8273
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.user.mymedicare/com.example.user.mymedicare.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.user.mymedicare.MainActivity.onCreate(MainActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-06 16:26:50.049 8273-8273/com.example.user.mymedicare I/Process: Sending signal. PID: 8273 SIG: 9
08-06 16:27:02.056 8388-8388/com.example.user.mymedicare W/System: ClassLoader referenced unknown path: /data/app/com.example.user.mymedicare-1/lib/x86
08-06 16:27:02.113 8388-8388/com.example.user.mymedicare W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-06 16:27:02.163 8388-8388/com.example.user.mymedicare D/AndroidRuntime: Shutting down VM
08-06 16:27:02.163 8388-8388/com.example.user.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.user.mymedicare, PID: 8388
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.user.mymedicare/com.example.user.mymedicare.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.user.mymedicare.MainActivity.onCreate(MainActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-06 16:27:04.722 8388-8388/com.example.user.mymedicare I/Process: Sending signal. PID: 8388 SIG: 9

如果有任何人可以提供帮助、阐明或建议开发想法,我会全力以赴,很高兴收到您的来信。

提前致谢。 =]

【问题讨论】:

能否提供错误日志? 当然可以。 =] "错误:找不到符号变量结果错误:找不到符号变量结果错误:找不到符号变量结果错误:找不到符号变量结果" 检查,你的xml文件有错误吗? 如我所见,我的 Manifest.xml 文件中没有错误。还有其他我应该查看和发布的文件吗? 查看res文件夹下的布局文件 【参考方案1】:

正如您在评论中所说,您有一个Activity_main.xml 文件。但是你注释掉的那行是setContentView(R.layout.MainLayout);

请取消注释此行,并将R.layout.MainLayout替换为

R.layout.Activity_main,清理你的项目,做必要的导入,希望它能工作

【讨论】:

不,不幸的是,这没有帮助。我创建的 MyMediCare 应用程序仍然无法在模拟器上运行。我同步、清理和更改了代码,但没有结果。 =( 你现在遇到什么错误?你能分享你的 Activity_main.xml 代码吗? Erm...应用程序无法运行...此外,.xml 是一个布局文件。没有代码。 这是一个布局文件。我已经提供了我认为与上述相关的所有代码。 对不起,我不明白你的意思。可能我需要了解更多信息或重新调整大脑以了解布局文件如何不是由代码组成的,而 xml 不是代码。快乐编程。【参考方案2】:

你为什么把setContentView(R.layout.MainLayout);注释掉

【讨论】:

因为模拟器不会运行它,但应用程序也不会运行。你能想到为什么 MainLayout 符号无法解析吗?即使我在 .xml 中正确识别了相对布局? 1)你确定你的布局文件有名字 MainLayout.xml (检查区分大小写)? 2)顺便尝试将其键入为 setContentView(com.example.user.medicare.R.mainlayout) @Mike Kain Markov 你试过我最后的建议了吗? 是的,我已经尝试通过对 setContentView 行的这些更改再次运行应用程序,模拟器仍然运行良好,但应用程序本身无法运行。布局文件称为activity_main.xml,该.xml 中的相关布局框称为“MainLayout”。所以,我有: setContentView(com.example.user.mymedicare.R.layout.activity_main);仍然无法在模拟器中运行 MyMediCare 应用程序。 错误:08/07 20:03:43:启动应用程序 $ adb push I:\Uni\Mobile Apps\Coursework2\MyMediCare\app\build\outputs\apk\app-debug.apk / data/local/tmp/com.example.user.mymedicare com.android.ddmlib.AdbCommandRejectedException: device 'emulator-5554' not found 安装 APK 时出错【参考方案3】:

您已经注释掉了 setContentView(R.layout.MainLayout); 行,这实际上使您的 Activity 没有 xml 膨胀布局。因此,您的下一行Button btnCalc = (Button) findViewById(R.id.btnCalc); 为您的btnCalc 对象返回null,该对象又在下一行被调用,这违反了Java 语言逻辑并通过抛出运行时NullPointerException 来停止程序执行

【讨论】:

谢谢你,我一开始没有注意到。现在 IDE 正试图告诉我符号“MainLayout”无法解析...这很令人困惑,因为它肯定在我的布局 .xml 中,但在代码后面的相同符号没有问题。【参考方案4】:

导入你的 R 类。

import com.example.youApp.R;

【讨论】:

感谢您的输入,但无法解析符号“youApp” 还有其他想法吗? =] 我是说为您的课程添加导入。例如。 “导入 com.example.user.mymedicare.R” 您的 id 存储在您的 R 类中。您无法解析符号,因为它们的 id 存储在 (R) 中的类未导入 啊,感谢您为我澄清这一点。我现在已经正确导入了它,但是它没有提供任何额外的错误或修复。符号“结果”仍未解决。 您从未声明变量“结果”哈哈。忽略你的代码,你会忘记一些小事。

以上是关于不幸的是,“应用程序”已经停止了 Android Studio 的问题的主要内容,如果未能解决你的问题,请参考以下文章

不幸的是,flutter_app 已经停止

Android自定义ArrayAdapter:滚动时“不幸的是,应用程序已停止”

不幸的是 <app><package> 已经停止在 android emulator-error ,如何解决这个问题?

不幸的是,应用程序已在某些 android 设备上停止

Android 应用程序错误 - 不幸的是,您的应用程序已停止

GCM Android:不幸的是,GCM 演示已停止