1

Ich habe eine Switch-Klasse, um Telefon oder Tablet zu bestimmen, und ich bekomme eine Nullzeiger-Ausnahme, wenn die Absicht erstellt wird. Ich wundere mich nur, was verursacht dies, da beide Aktivitäten vorhanden sind und der Switch funktioniert ordnungsgemäß als die Absicht, es auf Schaltern ausfällt, wenn auf einem Tablet-Telefon. HierNullPointerExeption mit und Intent Android

ist der Code für die erste Aktivität, die die jeweilige Aktivität startet:

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); 
} 

Hier ist die logcat für die Kraft nahe beim Start:

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

Ihr Quelltext Ihrer stacktrace entspricht nicht einen Fehler. Die Ausnahme tritt im Splashscreen-Konstruktor auf. Aber Ihre Quelle enthält den Konstruktor nicht. Bitte ändern Sie auch die Formatierung. Ich habe versucht, es zu verschönern, indem ich Registerkarten lösche und sie durch Leerzeichen ersetze. Aber es würde nicht summieren. –

Antwort

1

dies nicht in Verfahren verwendet werden kann, genannt vom Konstruktor, weil das Objekt noch nicht da ist. Sie sollten die Absicht in onCreate() erstellen. Kein Code (außer dem Konstruktor oder den statischen Initialisierern) der Aktivität wird vor onCreate() ausgeführt. Sie werden es also vor onCreate() nicht brauchen.

0

Ihr Code ist nicht richtig eingerückt, es ist schwierig zu verstehen, was Sie tun.

können Sie dies versuchen: (als @Dirk Jäckel vorgeschlagen) Verwendung

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; 
     } 
    } 
} 

sagen, wenn Sie noch