无法启动应用程序:YourApp已停止消息

时间:2014-06-18 05:07:23

标签: android

这是我收到的logcat输出。该应用程序几天前工作正常,但突然之间的应用程序无法在模拟器中启动。我尝试过多次创建新的模拟器。还尝试杀死进程adb.exe并重新启动它,但没有用。任何帮助表示赞赏 谢谢。

> 06-18 00:40:53.905: D/AndroidRuntime(898): Shutting down VM 06-18
> 00:40:53.905: W/dalvikvm(898): threadid=1: thread exiting with
> uncaught exception (group=0x41465700) 06-18 00:40:53.925:
> E/AndroidRuntime(898): FATAL EXCEPTION: main 06-18 00:40:53.925:
> E/AndroidRuntime(898): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}:
> java.lang.NullPointerException 06-18 00:40:53.925:
> E/AndroidRuntime(898):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> android.os.Handler.dispatchMessage(Handler.java:99) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> android.os.Looper.loop(Looper.java:137) 06-18 00:40:53.925:
> E/AndroidRuntime(898):    at
> android.app.ActivityThread.main(ActivityThread.java:5103) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> java.lang.reflect.Method.invokeNative(Native Method) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> java.lang.reflect.Method.invoke(Method.java:525) 06-18 00:40:53.925:
> E/AndroidRuntime(898):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> dalvik.system.NativeStart.main(Native Method) 06-18 00:40:53.925:
> E/AndroidRuntime(898): Caused by: java.lang.NullPointerException 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:32)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> android.app.Activity.performCreate(Activity.java:5133) 06-18
> 00:40:53.925: E/AndroidRuntime(898):  at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
> 06-18 00:40:53.925: E/AndroidRuntime(898):    ... 11 more 06-18
> 00:40:59.568: I/Process(898): Sending signal. PID: 898 SIG: 9 06-18
> 00:48:38.636: D/AndroidRuntime(919): Shutting down VM 06-18
> 00:48:38.636: W/dalvikvm(919): threadid=1: thread exiting with
> uncaught exception (group=0x41465700) 06-18 00:48:38.676:
> E/AndroidRuntime(919): FATAL EXCEPTION: main 06-18 00:48:38.676:
> E/AndroidRuntime(919): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}:
> java.lang.NullPointerException 06-18 00:48:38.676:
> E/AndroidRuntime(919):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> android.os.Handler.dispatchMessage(Handler.java:99) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> android.os.Looper.loop(Looper.java:137) 06-18 00:48:38.676:
> E/AndroidRuntime(919):    at
> android.app.ActivityThread.main(ActivityThread.java:5103) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> java.lang.reflect.Method.invokeNative(Native Method) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> java.lang.reflect.Method.invoke(Method.java:525) 06-18 00:48:38.676:
> E/AndroidRuntime(919):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> dalvik.system.NativeStart.main(Native Method) 06-18 00:48:38.676:
> E/AndroidRuntime(919): Caused by: java.lang.NullPointerException 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:32)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> android.app.Activity.performCreate(Activity.java:5133) 06-18
> 00:48:38.676: E/AndroidRuntime(919):  at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
> 06-18 00:48:38.676: E/AndroidRuntime(919):    ........... 11 more

[编辑]:主要活动代码

package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

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

        Button button1 = (Button)findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }



}

1 个答案:

答案 0 :(得分:0)

您的R.id.button1应该在fragment_main。因此,您必须像以下一样更改PlaceholderFragment

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        Button button1 = (Button) getView().findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "Hello", Toast.LENGTH_LONG).show();
            }
        });
    }
}

onCreate方法

中删除以下代码
Button button1 = (Button)findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
        }
    });

我希望这会对你有所帮助。