不幸的是,* app *已停止

时间:2014-05-30 13:21:41

标签: java android android-emulator

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

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_width="match_parent"
android:layout_height="match_parent"
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_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

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

logcat的

> 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 ComponentInfo{com.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

2 个答案:

答案 0 :(得分:0)

移动

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");
    }
});

进入onCreateView

PlaceholderFragment

button1textView1位于您fragment_main.xml Activity activity_main.xml <{1}}中{{1}}

答案 1 :(得分:0)

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

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");
    }
});