我有一个AsyncTask,它在名为BackgroundWorker.java的单独活动中工作。如果结果返回某个值,则此AsyncTask通过上下文将字符串发送到另一个名为Profile.java的活动,如果结果返回某个值,则该活动可以为int;如果结果为null或字符串,则可以为字符串“空”。
问题: 我总是在Profile.java活动的return_profile_infos()模块中获取该值,但是当代码尝试打开另一个活动时,应用程序崩溃,返回空对象引用。
这是我的代码:
BackgroundWorker.java
@Override
protected void onPostExecute(String result_id) {
super.onPostExecute(result_id);
if(typereceived.equals("rd_user_id")){
if (result_id!=null && (!"".equals(result_id))){
Profile dat = new Profile();
dat.return_profile_infos(result_id);
}
else{
result_id="empty";
Profile dat = new Profile();
dat.return_profile_infos(result_id);
}
}
}
Profile.java
public void return_profile_infos(String result_id) {
idtxt.setText(result_id);
idasync=result_id;
startActivity(new Intent(Profile.this,MainActivity.class));
}
logcat error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.otpauthentication, PID: 26434
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:130)
at android.content.Intent.<init>(Intent.java:5780)
at com.example.otpauthentication.Profile.return_profile_infos(Profile.java:138)
at com.example.otpauthentication.BackgroundWorker.onPostExecute(BackgroundWorker.java:167)
at com.example.otpauthentication.BackgroundWorker.onPostExecute(BackgroundWorker.java:25)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:130)
at android.content.Intent.<init>(Intent.java:5780)
at com.example.otpauthentication.Profile.return_profile_infos(Profile.java:138)
at com.example.otpauthentication.BackgroundWorker.onPostExecute(BackgroundWorker.java:167)
at com.example.otpauthentication.BackgroundWorker.onPostExecute(BackgroundWorker.java:25)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
E/EGL_emulation: tid 26471: swapBuffers(550): error 0x300d (EGL_BAD_SURFACE)
答案 0 :(得分:0)
构造函数Intent(Context,Class)
的第一个参数应为有效上下文。您不能直接创建上下文(如Profile dat = new Profile()
),而是必须将其作为参数传递给AsyncTask。这是example。