1

我在这一行收到空指针异常错误,这Log.d("Density",obj.getDensity()); 是整个代码:

public class MainActivity extends Activity {

AndroidUtil obj = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("Phone Model",AndroidUtil.PHONE_MODEL);
    Log.d("API Level","::"+AndroidUtil.APILEVEL);
    Log.d("Android Version",AndroidUtil.VERSION);
    Log.d("Code Name",AndroidUtil.CODE_NAME);
    Log.d("BootLoader",AndroidUtil.BOOT_LOADER);
    Log.d("Board",AndroidUtil.BOARD);
    Log.d("Brand",AndroidUtil.BRAND);
    Log.d("Manufacturer",AndroidUtil.MANUFACTURER);
    Log.d("Hardware",AndroidUtil.HARDWARE);
    //Log.d("Pixel Width","::"+obj.getWidthInPx());
    //Log.d("Pixel Height","::"+obj.getHeightInPx());
    //Log.d("Dp Width","::"+obj.getWidthInDp());
    //Log.d("Dp Height","::"+obj.getHeightInDp());
    Log.d("Density",obj.getDensity());
}

}

现在我的 AndroidUtil 课程在这里

public class AndroidUtil extends Activity {
    int Measuredwidth = 0;  
    int Measuredheight = 0;  
    Point size = new Point();

    WindowManager w = getWindowManager();

    static final int APILEVEL = android.os.Build.VERSION.SDK_INT;

    static final String PHONE_MODEL = android.os.Build.MODEL;

    static final String BOOT_LOADER = android.os.Build.BOOTLOADER;

    static final String BOARD = android.os.Build.BOARD;

    static final String VERSION = android.os.Build.VERSION.RELEASE;

    static final String CODE_NAME = android.os.Build.VERSION.CODENAME;

    static final String BRAND = android.os.Build.BRAND;

    static final String MANUFACTURER = android.os.Build.MANUFACTURER;

    static final String HARDWARE = android.os.Build.HARDWARE;   

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public int getHeightInPx(){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
              w.getDefaultDisplay().getSize(size);
              Measuredheight = size.y; 
         }else{
              Display d = w.getDefaultDisplay(); 
              Measuredheight = d.getHeight(); 
        }
        return Measuredheight;
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public int getWidthInPx(){
        Log.d("Width","called");
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
              w.getDefaultDisplay().getSize(size);
              Measuredwidth = size.x; 
         }else{
              Display d = w.getDefaultDisplay(); 
              Measuredwidth = d.getWidth(); 
        }
        return Measuredwidth;
    }

    public float getHeightInDp(){
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics ();
        display.getMetrics(outMetrics);

        float density  = getResources().getDisplayMetrics().density;
        float dpHeight = outMetrics.heightPixels / density;
        return dpHeight;
    }

    public float getWidthInDp(){
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics ();
        display.getMetrics(outMetrics);

        float density  = getResources().getDisplayMetrics().density;
        float dpWidth  = outMetrics.widthPixels / density;
        return dpWidth;
    }

    public String getDensity(){
        Log.d("density","called");
        String DPI = null;
        switch (getResources().getDisplayMetrics().densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            DPI = "Low Density";
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            DPI = "Medium Density";
            break;
        case DisplayMetrics.DENSITY_HIGH:
            DPI = "High Density";
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            DPI = "Extra High Density";
            break;
        }
        return DPI;
    }
}

记录猫错误:

    04-03 17:51:57.799: E/filePathInTheme(5781): fallback to res
04-03 17:51:57.829: E/filePathInTheme(5781): fallback to res
04-03 17:51:57.859: D/Phone Model(5781): A100
04-03 17:51:57.859: D/API Level(5781): ::15
04-03 17:51:57.859: D/Android Version(5781): 4.0.4
04-03 17:51:57.859: D/Code Name(5781): REL
04-03 17:51:57.859: D/BootLoader(5781): unknown
04-03 17:51:57.859: D/Board(5781): 7x27
04-03 17:51:57.859: D/Brand(5781): China Telecom
04-03 17:51:57.859: D/Manufacturer(5781): Micromax
04-03 17:51:57.859: D/Hardware(5781): qcom
04-03 17:51:57.869: W/dalvikvm(5781): threadid=1: thread exiting with uncaught exception (group=0x40a839d8)
04-03 17:51:57.869: E/AndroidRuntime(5781): FATAL EXCEPTION: main
04-03 17:51:57.869: E/AndroidRuntime(5781): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andutil/com.andutil.MainActivity}: java.lang.NullPointerException
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1961)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1152)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.os.Looper.loop(Looper.java:137)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread.main(ActivityThread.java:4450)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at java.lang.reflect.Method.invoke(Method.java:511)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at dalvik.system.NativeStart.main(Native Method)
04-03 17:51:57.869: E/AndroidRuntime(5781): Caused by: java.lang.NullPointerException
04-03 17:51:57.869: E/AndroidRuntime(5781):     at com.andutil.MainActivity.onCreate(MainActivity.java:28)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.Activity.performCreate(Activity.java:4465)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-03 17:51:57.869: E/AndroidRuntime(5781):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)
04-03 17:51:57.869: E/AndroidRuntime(5781):     ... 11 more

请帮忙 !!!

4

5 回答 5

7

像这样初始化活动不是一个好的选择(而且我很确定它在任何情况下都不起作用,它的上下文将始终为空)。在我看来,要实现您想要做的事情,最好的方法是创建一个 AndroidUtils 类,如下所示:

public class AndroidUtils {

     private Context mContext;

     public AndroidUtils(Context context){
          this.mContext = context;
     }

}

并且在任何地方你都需要一个上下文变量,getDensity()等等。使用mContext. 之后,您可以从任何 Activity 中使用此类,如下所示:

AndroidUtils mObject = new AndroidUtils(MainActivity.this);
mObject.getDensity();
于 2013-04-03T12:41:38.207 回答
1

您正在访问一个为 null 的活动,因此未初始化。

AndroidUtil obj = null;
...
Log.d("Density",obj.getDensity());
于 2013-04-03T12:33:29.017 回答
0

好吧,您确实分配nullobj

AndroidUtil obj = null;

如果你想要一个Activity实例,你必须做一些非常肮脏的黑客攻击。而不是保存对它的引用AndroidUtil和调用方法,你应该使用它Intents来传达你的Activities.

  1. 您可以使用startActivityForResult, 或
  2. 您可以Activity使用常规启动Intent,然后让它在全局对象上注册。

看看你的代码做了什么,这些都是坏主意。我只是将代码移动AndroidUtil到您的 mainActivity中。

如果您想将代码保留在 main 之外Activity,请将其实现为带有 as 参数的静态助手Activity,而不是通过 sublcasingActivity来访问其设施。

于 2013-04-03T12:35:34.400 回答
0

不是因为你声明对象为空吗?==>AndroidUtil obj = null;

于 2013-04-03T12:35:35.650 回答
0

只需添加这些代码

 String print = AndroidUtil.class.getDensity();

 Log.d("Density",print);
于 2013-04-03T13:08:33.793 回答