您好我刚开始学习在Android上开发。我的应用程序在每次启动之前都会停止。
我做了一些研究,所以我看看我的logcat。这是我的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)
我有一节课:
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在哪里?
非常感谢
答案 0 :(得分:0)
您应该创建两个活动。首次启动屏幕活动和第二次主要活动。我在你的清单文件中只看到一个。对SecondActivity.class使用intent。
最好用同步安全方法编写代码,例如:
private Thread mSplashThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
final SplashScreen sPlashScreen = this;
mSplashThread = new Thread(){
@Override
public void run(){
try {
synchronized(this){
wait(3000);
}
}
catch(InterruptedException ex){
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}