【发布时间】:2014-01-15 09:31:11
【问题描述】:
您好,我昨天刚开始学习在 android 上开发。我的应用程序在每次启动之前就停止了。
我做了一些研究,所以我查看了我的 logcat。这是我的日志:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
E/AndroidRuntime(26645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
E/AndroidRuntime(26645): at android.app.ActivityThread.access$700(ActivityThread.java:159)
E/AndroidRuntime(26645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
E/AndroidRuntime(26645): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(26645): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(26645): at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
我有 1 节课:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread logo = new Thread (){
public void run(){
try{
int logoTimer = 0;
while (logoTimer <= 5000){
sleep(100);
logoTimer = logoTimer + 100;
}
Intent startActivity = new Intent();
startActivity.setClassName("com.example.myfirstapp", "CLEARSCREEN");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
finish();
}
}
};
getActionBar().hide();
logo.run();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
有什么方法可以从我的 logcat 中指出我的 NPE 在哪里?
提前非常感谢
【问题讨论】:
-
尝试在你的线程构造函数中加入一个
Runnable。
标签: java android android-activity nullpointerexception