我在这一行收到空指针异常错误,这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
请帮忙 !!!