不幸的是 <appname> 已停止[重复]

Posted

技术标签:

【中文标题】不幸的是 <appname> 已停止[重复]【英文标题】:unfortunately <appname> has stopped [duplicate] 【发布时间】:2014-07-25 15:10:48 【问题描述】:

我在 eclipse.at 中编写了以下代码。起初它一直在工作,直到我在 MainActivity.java 文件中进行了一些更改,例如让按钮执行某些操作。当我运行应用程序时,出现以下错误消息:不幸的是已停止。请帮助我,谢谢。

MainActivity.java

package com.example.jony;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity 
int counter;
    Button add;
    Button sub;
    TextView tvdisplay;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        counter = 0;
        add =(Button) findViewById(R.id.badd);

        tvdisplay = (TextView) findViewById(R.id.tvdisplay);
        add.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                // TODO Auto-generated method stub
                counter = counter + 1;
                tvdisplay.setText("Your total is" + counter);
                        
        );
        sub.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                // TODO Auto-generated method stub
                counter = counter -1;
                tvdisplay.setText("Your total is" + counter);
            
        );
    

fragment_main.xml

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_
     android:layout_
     tools:context="com.example.jony.MainActivity$PlaceholderFragment"
     android:orientation="vertical" >

    <TextView
        android:layout_
        android:layout_
        android:textSize="25sp"
        android:gravity="center"
        android:text="@string/calci" />
    <Button
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:text="@string/add"
        android:textSize="35sp"
        android:id="@+id/badd" />
   <Button
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:text="@string/sub"
        android:textSize="35sp"
        android:id="@+id/bsub" />

</LinearLayout>

Activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_
    android:layout_
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

LogCat 输出:

06-05 00:42:13.452: E/Trace(1924): error opening trace file: No such file or directory (2)
06-05 00:42:14.492: D/AndroidRuntime(1924): Shutting down VM
06-05 00:42:14.492: W/dalvikvm(1924): threadid=1: thread exiting with uncaught       exception      (group=0xb5e2d288)
06-05 00:42:14.531: E/AndroidRuntime(1924): FATAL EXCEPTION: main
06-05 00:42:14.531: E/AndroidRuntime(1924): java.lang.RuntimeException: Unable to start    activity ComponentInfocom.example.jony/com.example.jony.MainActivity: java.lang.NullPointerException
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.os.Looper.loop(Looper.java:137)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invoke(Method.java:511)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at                    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at dalvik.system.NativeStart.main(Native Method)
06-05 00:42:14.531: E/AndroidRuntime(1924): Caused by: java.lang.NullPointerException
06-05 00:42:14.531: E/AndroidRuntime(1924):     at com.example.jony.MainActivity.onCreate(MainActivity.java:40)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.Activity.performCreate(Activity.java:5008)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-05 00:42:14.531: E/AndroidRuntime(1924):     at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-05 00:42:14.531: E/AndroidRuntime(1924):     ... 11 more

【问题讨论】:

您对堆栈跟踪/logcat 的哪一部分有疑问?您尝试过什么解决问题的方法? 【参考方案1】:

您的 xml 中没有 ID 为 tvdisplay 的 TextView,这是您尝试使用的设置

tvdisplay = (TextView) findViewById(R.id.tvdisplay);

因此,当您尝试在 onClick 中设置文本时,它会给您一个用于 tvdisplay 的 NPE。将您的 TextView xml 更改为

<TextView
    android:id="@+id/tvdisplay"
    android:layout_
    android:layout_
    android:textSize="25sp"
    android:gravity="center"
    android:text="@string/calci" />

【讨论】:

以上是关于不幸的是 <appname> 已停止[重复]的主要内容,如果未能解决你的问题,请参考以下文章

谁能帮助我的第一个 WebView 我被这个问题困住了?不幸的是 <app name> 已停止

不幸的是,“应用程序名称”已停止。 (安卓模拟器,Eclipse)

Android Studio Release build 给了我:不幸的是,“App Name”已经停止

不幸的是应用程序已停止

Android:更改视图顺序会导致“不幸的是,应用程序已停止”OK。为啥?

不幸的是,相机已停止