新手Android NullPointerException故障排除



我有一个类似的问题,这里的人:Android活动错误,我已经阅读了这篇文章。我也通读了其他的帖子。但我的Android应用程序似乎无法运行。

说明:我有一个按钮。我想让这个按钮工作。因为OnClickListener,我一直得到NPE。这实际上是一个与上面的帖子相同的问题,但我的行为有点不同。请帮帮我。非常感谢。

Main.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            startActivity(new Intent(Main.this, Second.class));
            }
        });
    }
}

Second.java

import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    }
}    
错误消息

12-27 09:19:10.729: W/dalvikvm(923): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-27 09:19:10.749: E/AndroidRuntime(923): FATAL EXCEPTION: main
12-27 09:19:10.749: E/AndroidRuntime(923): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harryoh.firstapp/com.harryoh.firstapp.Main}: java.lang.NullPointerException
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.os.Looper.loop(Looper.java:137)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-27 09:19:10.749: E/AndroidRuntime(923):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 09:19:10.749: E/AndroidRuntime(923):  at java.lang.reflect.Method.invoke(Method.java:511)
12-27 09:19:10.749: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 09:19:10.749: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 09:19:10.749: E/AndroidRuntime(923):  at dalvik.system.NativeStart.main(Native Method)
12-27 09:19:10.749: E/AndroidRuntime(923): Caused by: java.lang.NullPointerException
12-27 09:19:10.749: E/AndroidRuntime(923):  at com.harryoh.firstapp.Main.onCreate(Main.java:19)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.Activity.performCreate(Activity.java:4465)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-27 09:19:10.749: E/AndroidRuntime(923):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-27 09:19:10.749: E/AndroidRuntime(923):  ... 11 more
我真的很感谢你的帮助。我真的很想学习Android,而这些故障排除问题总是给我带来很多痛苦。编辑:谢谢你的帮助。一旦我有能力,我会回报这个社区。我还没有仔细看你的输入,因为我想按要求提供这个。我现在就开始做。我希望它能奏效。

main。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

这种情况在我搞布局的时候发生过很多次。如果您最近刚刚添加了按钮,请尝试刷新项目并清理重建,然后查看它是否拾取该按钮。由于某些原因,如果您添加小部件并尝试在它实际上没有更新后立即进行测试。

编辑:在看到你的main.xml之后,看起来你甚至没有一个按钮设置。这将导致空指针异常,因为它正在寻找不存在的东西。
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
``` setContentView(R.layout.second);
    }
}
prove this, create new xml with name second and add setContentView(R.layout.second) in the class Second 

首先,检查您是否在清单文件中包含了第二个活动。其次,没有与第二个活动相关联的布局。尝试添加第二个onCreate() setContentView(R.layout.main);

我猜你没有在Second活动中调用setContentView()

与其在代码中设置OnClick事件,不如在xml中设置它。

      <Button style="@style/WrapContent"
        android:onClick="buttonClicked"
        android:textSize="16dp"
        android:textStyle="bold"
        android:text="@string/button_text" />

然后将以下方法添加到您的活动中:

public void buttonClicked(View v) {
    // Handle Click Event
    startActivity(new Intent(this, Second.class) );
}

传入的View将是你的按钮的一个实例

你有没有在android manifest中声明你的第二个activity ?

 <activity
        android:label="@string/app_name"
        android:name=".Second" >
  </activity>

另外,您应该为第二个活动添加一个布局。确保在第二个活动的onCreate()中添加setContentView(R.layout.second)

现在已经提供了xml,注意这里没有button1,需要在调用findViewById()之前声明按钮。

检查完上面的内容后,尝试在你的xml中添加一个按钮,在你的线性布局

<Button android:id="@+id/button1"     
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

最新更新