2014-01-15 52 views
0

嗨我剛開始學習在android上開發昨天。我的應用在每次啓動之前都停止。無法啓動活動componentinfo java.lang.nullpointerexception是否需要幫助?

我做了一些研究,所以我看看我的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) 

我有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> 

有什麼辦法指出哪裏是我的NPE從我logcat的?

在此先感謝

+0

嘗試在您的線程構造函數中放置'Runnable'。 – csmckelvey

回答

0

你應該建立兩個活動這麼多。第一次啓動畫面活動和第二次主要活動。我在你的清單文件中只看到一個。將意圖用於SecondActivity.class。

而且與同步,例如安全的方法更好地寫代碼:

私人主題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();   
} 
相關問題