无法访问其他活动类的方法

时间:2013-04-03 12:28:27

标签: android methods

我在此行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;
    }
}

记录cat错误:

    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

请帮助!!!

5 个答案:

答案 0 :(得分:7)

初始化像这样的活动不是一个好的选择(我很确定它在任何情况下都不起作用,它的上下文总是为空)。为了实现你想要做的事情,我认为最好的方法是创建一个像这样的AndroidUtils类:

public class AndroidUtils {

     private Context mContext;

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

}

在任何地方,您需要在getDensity()等中使用上下文变量,并使用mContext。 完成后,您可以在任何活动中使用此类:

AndroidUtils mObject = new AndroidUtils(MainActivity.this);
mObject.getDensity();

答案 1 :(得分:1)

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

AndroidUtil obj = null;
...
Log.d("Density",obj.getDensity());

答案 2 :(得分:0)

嗯,您确实已将null分配给obj

AndroidUtil obj = null;

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

  1. 您可以使用startActivityForResult
  2. 您可以使用常规Activity启动Intent,然后让它在全局对象上注册。
  3. 看看你的代码做了什么,这些都是坏主意。我只需将AndroidUtil的代码移到您的主Activity

    如果要将代码保留在主Activity之外,请将其实现为以Activity为参数的静态帮助程序,而不是将Activity转换为访问其工具。< / p>

答案 3 :(得分:0)

C'est pas parceque tuasdéclarél'objetnull?

你不是声明一个对象null ==&gt; AndroidUtil obj = null;

答案 4 :(得分:0)

只需添加这些代码

 String print = AndroidUtil.class.getDensity();

 Log.d("Density",print);