onpostexecute中的intent的上下文不为null,但是获取null异常

时间:2017-05-24 16:27:37

标签: java android android-intent android-asynctask android-context

我已经被这个问题困扰了两天了。我已经检查了这个论坛和其他论坛,但无法得到足够接近我的问题的问题或答案。

基本上我正在尝试从非活动open an activity执行onpostexecute的意图,我从onMarkerClick函数发送上下文(MainActivty.this)和字符串这是MainActivity中的方法。这将转到非活动中的构造函数,该构造函数具有上下文和字符串的参数。

问题是我得到一个空异常,但是在调试之后,上下文不是null,它具有MainActivity的值,但是当执行intent时它返回一个null异常。我也尝试了许多变化,例如。 Activity,getApplicationContext,this.context,(classname).context,(classname).this并尝试了全局上下文无济于事。奇怪的是我将intent放入if语句if(context!= null)并且它通过并且它执行了intent,这反过来又给了我无效的null异常。我知道我是Android的新手,如果有人对开放活动有任何其他建议,非常感谢。

  

E / AndroidRuntime:致命异常:主要                                                                                       处理:com.example.quantusapps.joggertest,PID:12253                                                                                       java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.content.Context.getPackageName()'                                                                                           在android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)                                                                                           在android.content.ComponentName。(ComponentName.java:77)                                                                                           在android.content.Intent。(Intent.java:4029)                                                                                           at com.example.quantusapps.joggertest.BusCoachTramInfo.onPostExecute(BusCoachTramInfo.java:131)                                                                                           at com.example.quantusapps.joggertest.BusCoachTramInfo.onPostExecute(BusCoachTramInfo.java:25)                                                                                           在android.os.AsyncTask.finish(AsyncTask.java:632)                                                                                           在android.os.AsyncTask.access $ 600(AsyncTask.java:177)                                                                                           在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                           在android.os.Looper.loop(Looper.java:155)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5696)                                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                                           在java.lang.reflect.Method.invoke(Method.java:372)                                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1028)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)<

这是onMarkerClick的主要活动方法

mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {


                String bctID = bctExtraMarkerInfo.get(marker.getId());
                BusCoachTramInfo busCoachTramInfo = new BusCoachTramInfo(bctID, MainActivity.this);
                busCoachTramInfo.execute(bctID);

                return false;
            }
        });

这是非活动构造函数。

    Context context;

    BusCoachTramInfo(String busstopID, Context context) {

    this.context = context;

    naptanIdUrl = "https://api.tfl.gov.uk/StopPoint/" + busstopID + "/Arrivals?app_key=" + tfl_API_KEY + "&app_id=9c0b3009";



}

这是发生null异常的地方。

   @Override
protected void onPostExecute(TreeMap<Integer, String[]> Map) {
    super.onPostExecute(Map);

    Intent i = new Intent(context, BusArrivalTime.class);
    context.startActivity(i);

1 个答案:

答案 0 :(得分:0)

完成任务的一种方法是实现AsyncTask作为方法的一部分,该方法将MainActivity的实例作为参数。另一方面,AsyncTask适用于WeakReference

void doExecuteBusCoachTramInfo(final String busstopID, MainActivity activity){

    final WeakReference<MainActivity> wrActivity = new WeakReference<MainActivity>(MainActivity.this);
    new AsyncTask<Void, Void, TreeMap<Integer, String[]>>(){

        @Override
        protected TreeMap<Integer, String[]> doInBackground(Void... params)
        {
            // your code from BusCoachTramInfo here
        }

        @Override
        protected void onPostExecute(TreeMap<Integer, String[]> integerTreeMap)
        {
            // get back to the original Activity if possible:
            MainActivity activity = wrActivity.get();
            if (activity != null){
                Intent i = new Intent(activity, BusArrivalTime.class);
                activity.startActivity(i);
            }
        }
    }.execute();
}

此方法可能是MainActivity的一部分,但它也可以属于其他类。