java - 新手 Android NullPointerException 故障排除

标签 java android exception

我遇到了与这里的人类似的问题:Android activity error我已通读该帖子。我还阅读了其他帖子。但我似乎无法让我的 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));
            }
        });
    }
}

第二.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 后,看起来你甚至没有设置按钮。这会导致空指针异常,因为它正在寻找不存在的东西。

关于java - 新手 Android NullPointerException 故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647553/

相关文章:

android - 致命异常 : unknown error (code 14) could not open database

java - 使用 Apache Derby 运行 TestDB 程序 - java.sql.SQLException : The url cannot be null

java - 请求的资源 (/) 不可用

java - sleep 但释放锁

android - LinearLayout、RelativeLayout 等边距不按预期工作

java - 从构造函数抛出未经检查的异常时面临 java.lang.StackOverflowError

objective-c - Cocoa的try/catch/finally异常处理在什么情况下@finally是非冗余的?

java - 如何从java启动chrome浏览器

java - 如何确定 Java 字符串的子字符串是否与正则表达式匹配?

Android 网络传输对象