不幸的是,*app* 已停止 [重复]

Posted

技术标签:

【中文标题】不幸的是,*app* 已停止 [重复]【英文标题】:Unfortunately, *app* has stopped [duplicate] 【发布时间】:2014-07-20 06:38:45 【问题描述】:

我是编程新手。我一开始就卡住了...当我尝试在模拟器上运行时,模拟器启动但应用程序无法打开。任何人都可以解决这个问题.. 谢谢!

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView tv = (TextView) findViewById(R.id.textView1);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            tv.setText("JustNope");
        
    );

    if (savedInstanceState == null) 
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.worrkkkk.grrrrr.MainActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button1"
    android:layout_
    android:layout_
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_
    android:layout_
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="24dp"
    android:text="Nope"
    android:textAppearance="?android:attr/textAppearanceLarge" />

日志猫

> 05-30 11:00:58.741: I/dalvikvm(569): threadid=3: reacting to signal 3
05-30 11:00:59.009: I/dalvikvm(569): Wrote stack traces to '/data/anr/traces.txt'
05-30 11:00:59.132: D/AndroidRuntime(569): Shutting down VM
05-30 11:00:59.132: W/dalvikvm(569): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-30 11:00:59.141: E/AndroidRuntime(569): FATAL EXCEPTION: main
05-30 11:00:59.141: E/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfocom.worrkkkk.grrrrr/com.worrkkkk.grrrrr.MainActivity: java.lang.NullPointerException
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.os.Looper.loop(Looper.java:137)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 11:00:59.141: E/AndroidRuntime(569):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 11:00:59.141: E/AndroidRuntime(569):  at java.lang.reflect.Method.invoke(Method.java:511)
05-30 11:00:59.141: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 11:00:59.141: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 11:00:59.141: E/AndroidRuntime(569):  at dalvik.system.NativeStart.main(Native Method)
05-30 11:00:59.141: E/AndroidRuntime(569): Caused by: java.lang.NullPointerException
05-30 11:00:59.141: E/AndroidRuntime(569):  at com.worrkkkk.grrrrr.MainActivity.onCreate(MainActivity.java:25)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.Activity.performCreate(Activity.java:4465)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-30 11:00:59.141: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-30 11:00:59.141: E/AndroidRuntime(569):  ... 11 more
05-30 11:00:59.291: I/dalvikvm(569): threadid=3: reacting to signal 3
05-30 11:00:59.311: I/dalvikvm(569): Wrote stack traces to '/data/anr/traces.txt'
05-30 11:00:59.711: I/dalvikvm(569): threadid=3: reacting to signal 3
05-30 11:00:59.721: I/dalvikvm(569): Wrote stack traces to '/data/anr/traces.txt'
05-30 11:01:00.901: I/Process(569): Sending signal. PID: 569 SIG: 9

【问题讨论】:

MainActivity 的第 25 行是什么? 转到这个http://***.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046 【参考方案1】:

移动

final TextView tv = (TextView) findViewById(R.id.textView1);

Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() 

    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
        tv.setText("JustNope");
    
);

进入你的onCreateViewPlaceholderFragment

button1textView1fragment_main.xml 中,而在您的 Activity 中,您正在膨胀 activity_main.xml

【讨论】:

来吧,你现在应该知道它是***.com/questions/23653778/…的复制品【参考方案2】:

这是因为您在错误的地方编写代码。 编写片段中的代码如下所示:

final TextView tv = (TextView)rootview.findViewById(R.id.textView1);

Button btn = (Button) rootview.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() 

    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
        tv.setText("JustNope");
    
);

【讨论】:

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

当我按下一个按钮时,“不幸的是我的应用程序已停止”[重复]

当我按下 ButtonImage 时,应用程序崩溃了。不幸的是,该应用程序已停止工作[重复]

不幸的是,flutter_app 已经停止

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

不幸的是,在 Android 的 layout-land 文件中更改 .xml 时,应用程序已在物理设备上停止 [重复]

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