1

我有一個開關類來確定手機或平板電腦,當意圖創建時,我得到一個空指針異常。我只是想知道是什麼導致這種情況,因爲這兩種活動都存在,並且交換機正常工作,因爲它在平板電腦的手機上切換出錯的意圖。NullPointerExeption與意圖Android

這裏是啓動相應的活動的初始活性代碼:

package jack.beastapps.TimerPlus; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 

public class SplashScreen extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 


public boolean isTablet() { 
try { 
    Context context = this; 
    // Compute screen size 
    DisplayMetrics dm = context.getResources().getDisplayMetrics(); 
    float screenWidth = dm.widthPixels/dm.xdpi; 
    float screenHeight = dm.heightPixels/dm.ydpi; 
    double size = Math.sqrt(Math.pow(screenWidth, 2) + 
          Math.pow(screenHeight, 2)); 

    // Tablet devices should have a screen size greater than 6 inches 
    return size >= 6; 
} catch(Throwable t) { 
    return false; 
} 
}{ 
if (isTablet() == true) { 
     Intent tablet = new Intent(SplashScreen.this, TabletActivity.class); 
     startActivity(tablet); 
} 
else { 
     Intent phone = new Intent(SplashScreen.this, PhoneActivity.class); 
     startActivity(phone); 
} 

下面是密切在啓動時的力的logcat:

09:34:08.454: E/AndroidRuntime(12322): FATAL EXCEPTION: main 
04-07 09:34:08.454: E/AndroidRuntime(12322): java.lang.RuntimeException: Unable to   instantiate activity   
ComponentInfo{jack.beastapps.TimerPlus/jack.beastapps.TimerPlus.SplashScreen}: java.lang.NullPointerException 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.os.Looper.loop(Looper.java:137) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at java.lang.reflect.Method.invoke(Method.java:511) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at dalvik.system.NativeStart.main(Native Method) 
04-07 09:34:08.454: E/AndroidRuntime(12322): Caused by: java.lang.NullPointerException 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.content.ComponentName.<init>(ComponentName.java:75) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.content.Intent.<init>(Intent.java:3122) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at jack.beastapps.TimerPlus.SplashScreen.<init>(SplashScreen.java:36) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at java.lang.Class.newInstanceImpl(Native Method) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at java.lang.Class.newInstance(Class.java:1319) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
04-07 09:34:08.454: E/AndroidRuntime(12322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 
04-07 09:34:08.454: E/AndroidRuntime(12322): ... 11 more 
+0

你的源代碼不符合您的堆棧跟蹤。 Splashscreen構造函數中發生異常。但是你的源碼不包含構造函數。請也改變格式。我試圖通過刪除標籤並用空格替換它們來美化它。但它不會加起來。 – 2012-06-12 18:48:00

回答

1

這不能的方法使用名爲從構造函數中,因爲該對象還沒有。你應該在onCreate()中創建Intent。在Activity之前沒有代碼(構造函數或靜態初始化器除外)在onCreate()之前運行。所以你不會在onCreate()之前需要它。

0

您的代碼沒有正確縮進,很難理解您的操作。

你可以試試這個:(如@Dirk Jackel公司建議)

package jack.beastapps.TimerPlus; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 

public class SplashScreen extends Activity { 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     if (isTablet() == true) { 
      Intent tablet = new Intent(SplashScreen.this, TabletActivity.class); 
      startActivity(tablet); 
     } 
     else { 
      Intent phone = new Intent(SplashScreen.this, PhoneActivity.class); 
      startActivity(phone); 
     } 
    } 


    public boolean isTablet() 
    { 
     try { 
      Context context = this; 
      // Compute screen size 
      DisplayMetrics dm = context.getResources().getDisplayMetrics(); 
      float screenWidth = dm.widthPixels/dm.xdpi; 
      float screenHeight = dm.heightPixels/dm.ydpi; 
      double size = Math.sqrt(Math.pow(screenWidth, 2) + 
         Math.pow(screenHeight, 2)); 

      // Tablet devices should have a screen size greater than 6 inches 
      return size >= 6; 
     } catch(Throwable t) { 
      return false; 
     } 
    } 
} 

泰爾使用,如果你仍然得到一個錯誤