空指针异常错误,我无法弄明白

时间:2012-12-19 09:59:06

标签: android nullpointerexception

我是编程新手,我花了两天时间试图找出我的程序出了什么问题。我学到了很多东西试图解决这个问题,但我没有得到任何结论。

我无法弄清楚如何在eclipse中复制logcat中的错误,所以我会输出错误:

 Caused by: java.lang.NullPointerException at com.deweyx.MainActivity.OnCreate(MainActivity.java:27)

这是我的代码:

public class MainActivity extends Activity {

    int counter;
    Button add, sub;
    TextView display;

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

        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.xDisplay);

        add.setOnClickListener(new View.OnClickListener() {

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

        sub.setOnClickListener(new View.OnClickListener() {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这是xml文件夹:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >



<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/display" 
    android:layout_gravity="center"
    android:textSize="40sp"
    android:id="@+id/xDisplay"
/>


<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/padd"
    android:layout_gravity="center"
    android:textSize="20sp"
    android:id="@+id/bAdd"
 />


<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/psub"
    android:layout_gravity="center"
    android:textSize="20sp" 
    android:id="@+id/bSub"
/>

</LinearLayout>

logcat的:

   12-19 02:11:36.423: D/AndroidRuntime(531): Shutting down VM
12-19 02:11:36.423: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:36.444: E/AndroidRuntime(531): FATAL EXCEPTION: main
12-19 02:11:36.444: E/AndroidRuntime(531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:137)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:36.444: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:36.444: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531): Caused by: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:36.444: E/AndroidRuntime(531):  ... 11 more
12-19 02:11:36.653: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.673: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:36.983: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.993: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.452: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.502: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.783: D/AndroidRuntime(551): Shutting down VM
12-19 02:11:52.783: W/dalvikvm(551): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:52.793: E/AndroidRuntime(551): FATAL EXCEPTION: main
12-19 02:11:52.793: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.os.Looper.loop(Looper.java:137)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:52.793: E/AndroidRuntime(551):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:52.793: E/AndroidRuntime(551):  at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:52.793: E/AndroidRuntime(551):  ... 11 more
12-19 02:11:52.963: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.963: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:53.343: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:53.353: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'

3 个答案:

答案 0 :(得分:2)

您想学习定义按钮点击的快捷方式吗?

这是为任何按钮定义点击侦听器的快捷方式。

步骤1:在添加按钮xml中包含android:onClick =“btnAddButtonClick”:

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/padd"
    android:layout_gravity="center"
    android:textSize="20sp"
    android:onClick="btnAddButtonClick"     <<----- android:onClick
    android:id="@+id/bAdd"
 />

第2步:在您的活动中创建一个方法,其名称与您在onClick属性中给出的值相同:

public void btnAddButtonClick(View v) 
{
     counter++;
     display.setText("Your total is " + counter);   
}

答案 1 :(得分:0)

一切都是正确的,只需Project->Clean

如果它没有帮助您,则需要重新启动IDE!

答案 2 :(得分:0)

因为在查找id bAdd时它在findViewById上崩溃,这可能是因为该id尚未定义。你确定你使用的是正确的xml吗?我的意思是,你确定你使用activity_main.xml作为XML布局吗?因为您可能正在查找错误的XML文件,所以发生在我身上: - )