【问题标题】:NullPointerException with and intent AndroidNullPointerException 和意图 Android
【发布时间】:2012-04-07 16:51:54
【问题描述】:

我有一个开关类来确定手机或平板电脑,并且在创建意图时出现空指针异常。我只是想知道是什么导致了这种情况,因为这两个活动都存在并且开关正常工作,因为它在平板电脑手机上时在开关上出错的意图。

这是启动相应活动的初始活动的代码:

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

【问题讨论】:

  • 您的源代码与您的堆栈跟踪不匹配。异常发生在 Splashscreen 构造函数中。但是您的来源不包括构造函数。请同时更改格式。我试图通过删除标签并用空格替换它们来美化它。但它不会加起来。

标签: android android-intent nullpointerexception runtimeexception


【解决方案1】:

this 不能在从构造函数调用的方法中使用,因为对象还不存在。您应该在 onCreate() 中创建 Intent。在 onCreate() 之前没有运行来自 Activity 的代码(除了构造函数或静态初始化程序)。所以在 onCreate() 之前你不需要它。

【讨论】:

    【解决方案2】:

    你的代码没有正确缩进,很难理解你做了什么。

    你能试试这个吗:(正如@Dirk Jäckel 建议的那样)

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

    如果仍然出现错误,请告诉使用

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2014-05-31
      • 2012-09-06
      相关资源
      最近更新 更多