我正在創建一個2人遊戲反應應用程序,試圖學習一些關於Java/Android的新東西(比如線程),並且意識到每當我嘗試打開該應用程序時,它就會進入黑屏並立即說出「你的APP沒有迴應」(或類似的東西),儘管我幾乎沒有編碼任何東西。爲什麼我的應用程序在打開時立即崩潰?
我的MainActivity類別:
public class MainActivity extends Activity implements View.OnClickListener {
Button player1, player2;
Random random;
Double color;
Thread timer;
long x, elapsed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVar();
game();
}
private void game() {
color = random.nextDouble();
x = System.currentTimeMillis();
}
private void initializeVar() {
Button player1 = (Button) findViewById(R.id.bPlayer1);
Button player2 = (Button) findViewById(R.id.bPlayer2);
player1.setOnClickListener(this);
player2.setOnClickListener(this);
player1.getBackground().setColorFilter(0xFFFF0000,
PorterDuff.Mode.MULTIPLY);
player2.getBackground().setColorFilter(0xFFFF0000,
PorterDuff.Mode.MULTIPLY);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bPlayer1:
elapsed = System.currentTimeMillis();
elapsed = elapsed - x;
if (elapsed < 10) {
} else {
}
break;
case R.id.bPlayer2:
elapsed = System.currentTimeMillis();
elapsed = elapsed - x;
if (elapsed < 10) {
} else {
}
break;
}
}
}
活動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=".MainActivity" >
<Button
android:id="@+id/bPlayer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bPlayer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meteorfiber.reaction"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.meteorfiber.reaction.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MY logcat的:
02-20 20:27:54.803: E/AndroidRuntime(1747): FATAL EXCEPTION: main
02-20 20:27:54.803: E/AndroidRuntime(1747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meteorfiber.reaction/com.meteorfiber.reaction.MainActivity}: java.lang.NullPointerException
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.os.Looper.loop(Looper.java:137)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 20:27:54.803: E/AndroidRuntime(1747): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 20:27:54.803: E/AndroidRuntime(1747): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 20:27:54.803: E/AndroidRuntime(1747): at dalvik.system.NativeStart.main(Native Method)
02-20 20:27:54.803: E/AndroidRuntime(1747): Caused by: java.lang.NullPointerException
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.meteorfiber.reaction.MainActivity.game(MainActivity.java:28)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.meteorfiber.reaction.MainActivity.onCreate(MainActivity.java:24)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.Activity.performCreate(Activity.java:4465)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
logcat的說,你在第28行有一個空指針在MainActivity.java文件;也許這就是問題所在? – ryanbwork 2013-02-21 01:56:02