不幸的是,app停止在Android模拟器中工作

时间:2014-05-16 16:16:45

标签: java android

UI部分单独工作正常,但后来当我编写OnClickListener并运行代码时,我收到一条错误消息,指出“遗憾的是应用停止了工作”。

我再次尝试了一个新项目我收到了这些错误。我已经附加了我的XML,MainActivity类和日志。请帮助我。我一直坚持这个简单的程序一整个星期。 :(

日志:

 05-18 01:54:31.015: D/Android :(1436): The onCreate() event
    05-18 01:54:31.015: D/AndroidRuntime(1436): Shutting down VM
    05-18 01:54:31.015: W/dalvikvm(1436): threadid=1: thread exiting with uncaught  exception (group=0xb2afaba8)
    05-18 01:54:31.055: E/AndroidRuntime(1436): FATAL EXCEPTION: main
    05-18 01:54:31.055: E/AndroidRuntime(1436): Process: com.example.test3, PID: 1436
    05-18 01:54:31.055: E/AndroidRuntime(1436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test3/com.example.test3.MainActivity}: java.lang.NullPointerException
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.os.Handler.dispatchMessage(Handler.java:102)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.os.Looper.loop(Looper.java:136)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at java.lang.reflect.Method.invoke(Method.java:515)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at dalvik.system.NativeStart.main(Native Method)
    05-18 01:54:31.055: E/AndroidRuntime(1436): Caused by: java.lang.NullPointerException
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.example.test3.MainActivity.onCreate(MainActivity.java:25)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.Activity.performCreate(Activity.java:5231)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     ... 11 more




---

我不确定我的MainActivity.java是否有任何问题。我在这里附上我的XMl文件和MainActivity.java以获得清晰的图片。

MainActivity.java:

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
       String msg = "Android : ";
       int counter;
       Button add,sub;
       TextView display;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.d(msg, "The onCreate() event");
          counter=0;
          add=(Button) findViewById(R.id.addButton);
          sub=(Button) findViewById(R.id.subButton);
          display=(TextView) findViewById(R.id.disp);
          add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Count is "+counter);

            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Count is "+counter);
            }
        });

       }


    }

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.example.test3.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/subButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/disp"
        android:layout_toRightOf="@+id/addButton"
        android:text="Sub 1" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="14dp"
        android:text="Add 1" />

    <TextView
        android:id="@+id/disp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Count is 0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:2)

可能原因(猜测)视图属于片段布局。因此,应该在片段onCreateView中初始化视图,如下所示。

我注意到的另一件事是你在点击两次按钮时都counter++。点击子按钮

可能应为count--
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
      counter=0;
      add=(Button) rootView.findViewById(R.id.Add);
      sub=(Button) rootView.findViewById(R.id.Sub);
      disp=(TextView) rootView.findViewById(R.id.Display);
      add.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        counter++;
        disp.setText("counter is "+counter);

     }
     });

    sub.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        counter++; 
        disp.setText("counter is "+counter);

    }
   });

    return rootView;
}