Android应用程序在重启时崩溃

时间:2014-03-21 16:19:04

标签: java android crash

启动应用程序时,它会在以下方法中出现java null point错误

 private ParamHandler(Context context) {
      Log.e("ParamHandler : Constructor : Enter"); 
        mContext = context.getApplicationContext();
        mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        mVersionedPsc = VersionedPsc.getInstance(mTm);
        msisdn = getPhoneNumber();
        Log.e("ParamHandler : Constructor : Exit"); 
    }

    public static synchronized ParamHandler getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new ParamHandler(context);
        }
        return mInstance;
}

这是我的logcat文件。此行号45和55给出了零点错误

03-20 07:07:10.662: E/AndroidRuntime(1240): FATAL EXCEPTION: Thread-83
03-20 07:07:10.662: E/AndroidRuntime(1240): Process: com.instantmeter.starter, PID: 1240
03-20 07:07:10.662: E/AndroidRuntime(1240): java.lang.NullPointerException
03-20 07:07:10.662: E/AndroidRuntime(1240):     at com.mobile.instantmeter.service.ParamHandler.<init>(ParamHandler.java:45)
03-20 07:07:10.662: E/AndroidRuntime(1240):     at com.mobile.instantmeter.service.ParamHandler.getInstance(ParamHandler.java:55)
03-20 07:07:10.662: E/AndroidRuntime(1240):     at com.mobile.instantmeter.service.MasterThread.run(MasterThread.java:110)
03-20 07:07:10.662: E/AndroidRuntime(1240):     at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

请务必在context方法中发送/接收ParamHandler(Context context),可能context为空。那么你不需要在应用程序上下文中设置mContext变量。

 private ParamHandler(Context context) {
      Log.e("ParamHandler : Constructor : Enter"); 
       //mContext = context.getApplicationContext();
        mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        mVersionedPsc = VersionedPsc.getInstance(mTm);
        msisdn = getPhoneNumber();
        Log.e("ParamHandler : Constructor : Exit"); 
    }

使用包含应用程序上下文的mContext变量的其他选项。

private Context mContext;

  private ParamHandler() {
       Log.e("ParamHandler : Constructor : Enter"); 
        mContext = getApplicationContext();
        mTm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        mVersionedPsc = VersionedPsc.getInstance(mTm);
        msisdn = getPhoneNumber();
        Log.e("ParamHandler : Constructor : Exit"); 
    }