Android开发 - 按钮抛出空指针异常

时间:2014-04-08 05:42:51

标签: java xml android

我已经查看了之前的几个问题,但未能找到解决问题的答案。我正在创建一个简单的程序,我的按钮抛出空指针异常。起初我以为这是xml文件,eclipse在我修改字段(不在id部分)时随时添加@ + id /任何内容,但是当我修复这些错误时它仍然会这样做。不知道它可能是什么,我附上了代码和xml文件。

感谢您的帮助。

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    Button submit = (Button) findViewById(R.id.button1);
}

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="134dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="93dp"
        android:text="Enter a number" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/textView1"
        android:layout_below="@id/editText1"
        android:layout_marginTop="17dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

编辑:LogCat

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

1 个答案:

答案 0 :(得分:0)

布局是tools:context="com.example.myproc.MainActivity$PlaceholderFragment"属性建议的片段布局。活动onCreate()对于查找活动findViewById()的片段视图来说为时尚早。

将需要访问片段视图的代码移动到该片段的onCreateView():膨胀您的片段布局,根据需要在膨胀的布局上调用findViewById()并返回布局。