来自 findViewById 的 NullPointer 错误

Posted

技术标签:

【中文标题】来自 findViewById 的 NullPointer 错误【英文标题】:NullPointer error from findViewById 【发布时间】:2013-01-02 18:44:47 【问题描述】:

我的 findViewById() 上的空指针有问题 它是一个非常简单的应用程序,它首先加载到 AnalogClock 活动中,只需一个模拟时钟和按钮即可转到 DigitalClock 活动,第一个活动加载正常,但是当单击按钮时,DigitalClock 活动的 onCreate 中的 viewById() 返回 null 并且然后崩溃。'

AnalogActivity.java

package com.example.dualclocks2;

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

public class AnalogActivity extends Activity 
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analog);

    Button go2DigitalButton = (Button) findViewById(R.id.btnGo2Digital);
    go2DigitalButton.setOnClickListener(go2DigitalButtonListener);



public OnClickListener go2DigitalButtonListener = new OnClickListener() 
   

    @Override
    public void onClick(View v) 


         Intent intent = new Intent(AnalogActivity.this, DigitalActivity.class);            

         startActivity(intent); // execute the Intent
    

   ;

@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_analog, menu);
    return true;


activity_analog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".AnalogActivity" >

<AnalogClock
    android:id="@+id/analogClock1"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="150dp" />

<Button
    android:id="@+id/btnGo2Digital"
    android:layout_
    android:layout_
    android:layout_above="@+id/analogClock1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="62dp"
    android:text="@string/go2DigitalClock" />
</RelativeLayout>

DigitalActivity.java

    package com.example.dualclocks2;

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

public class DigitalActivity extends Activity 
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digital);

    Button go2AnalogButton = (Button) findViewById(R.id.btnGo2Digital);//null here
    go2AnalogButton.setOnClickListener(go2AnalogButtonListener);


public OnClickListener go2AnalogButtonListener = new OnClickListener() 
   

    @Override
    public void onClick(View v) 


         Intent intent = new Intent(DigitalActivity.this, AnalogActivity.class);            

         startActivity(intent); // execute the Intent
    

   ;

@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_digital, menu);
    return true;

activity_digital.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".DigitalActivity" >

<Button
    android:id="@+id/btnGo2Analog"
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="69dp"
    android:text="@string/go2Analog" />

<DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_
    android:layout_
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="DigitalClock"
    android:textSize="@dimen/digitalClockSize" />

</RelativeLayout>

LogCat

01-18 17:05:35.835: E/AndroidRuntime(15800): FATAL EXCEPTION: main
01-18 17:05:35.835: E/AndroidRuntime(15800): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.dualclocks2/com.example.dualclocks2.DigitalActivity: java.lang.NullPointerException
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.os.Looper.loop(Looper.java:130)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.main(ActivityThread.java:3688)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at java.lang.reflect.Method.invoke(Method.java:507)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at dalvik.system.NativeStart.main(Native Method)
01-18 17:05:35.835: E/AndroidRuntime(15800): Caused by: java.lang.NullPointerException
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.example.dualclocks2.DigitalActivity.onCreate(DigitalActivity.java:19)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-18 17:05:35.835: E/AndroidRuntime(15800):    ... 11 more

【问题讨论】:

【参考方案1】:
R.layout.activity_digital

不包含 id 为 btnGo2Digital 的 Button。

看来你想要R.id.btnGo2Analog 并打算写

Button go2AnalogButton = (Button) findViewById(R.id.btnGo2Analog);//changed id

【讨论】:

/facepalm 感谢您的帮助,显然我和我的教练需要检查我们的眼睛:P @user1681155 没问题,有类似的布局和名称有时可能会对您造成影响。现在你知道要检查什么了:-)

以上是关于来自 findViewById 的 NullPointer 错误的主要内容,如果未能解决你的问题,请参考以下文章

Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi

Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi

Android findViewById空指针[重复]

在 ListView 中显示来自 url 的可绘制图像

Android数据绑定DataBinding入门篇

来自画廊的照片旋转 90 度和小问题