0


我的应用程序中有一个奇怪的错误,有时在下面的代码中出现 NULL 异常(是的,有时听起来很奇怪!!!)

请看一看

String appId = preferences.getString(ProjectConstants.IMEI_NO, null);
    if (appId == null) {
        // Here i am getting exception:
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
        appId = manager.getDeviceId();
        preferences.edit().putString(ProjectConstants.IMEI_NO, appId);
    }

日志猫

    java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:278)
 at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 at java.lang.Thread.run(Thread.java:856) Caused by: java.lang.NullPointerException
 at com.homeshop18.service.GSONfunctions.getGSONfromURL(GSONfunctions.java:115)
 at com.homeshop18.ui.myorder.MyOrdersFragment$DownloadMyOrders.doInBackground(MyOrdersFragment.java:92)
 at com.homeshop18.ui.myorder.MyOrdersFragment$DownloadMyOrders.doInBackground(MyOrdersFragment.java:1)
 at android.os.AsyncTask$2.call(AsyncTask.java:264)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) ... 5 more

任何帮助将不胜感激.. :D

4

1 回答 1

0

您的 logcat 日志清楚地显示,

在 com.homeshop18.service.GSONfunctions.getGSONfromURL(GSONfunctions.java:115)

请在第 115 行检查,有一些 null 并且您正在尝试访问它的属性。

更新,如果您的第一个代码片段来自GSONfunctions,您必须检查是否context为空。

于 2016-08-29T18:35:42.217 回答