我的 findViewById() 上的空指针有问题,它是一个非常简单的应用程序,它首先加载到 AnalogClock 活动中,只有一个模拟时钟和按钮进入 DigitalClock 活动,第一个活动加载正常,但是当单击按钮时 viewById () 在 DigitalClock 活动的 onCreate 中返回 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;
}
}
活动模拟.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"
tools:context=".AnalogActivity" >
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp" />
<Button
android:id="@+id/btnGo2Digital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/analogClock1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:text="@string/go2DigitalClock" />
</RelativeLayout>
数字活动.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_width="match_parent"
android:layout_height="match_parent"
tools:context=".DigitalActivity" >
<Button
android:id="@+id/btnGo2Analog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:text="@string/go2Analog" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="DigitalClock"
android:textSize="@dimen/digitalClockSize" />
</RelativeLayout>
日志猫
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 ComponentInfo{com.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