[英]nullpointer exception onPostExecute method in AsyncTask
package com.testgcm;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.TextView;
public class SigninActivity extends AsyncTask<String,Void,String> {
private TextView statusField;
private Context context;
private ProgressDialog progress;
private MainActivity act;
public AsyncResponse delegate=null;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,ProgressDialog progress,int flag) {
this.context = context;
this.statusField = statusField;
this.progress=progress;
}
protected void onPreExecute(){
progress.show();
}
protected String doInBackground(String arg0[]) {
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link="http://192.168.43.160/Studentadministration/login_check.php";
String data = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
protected void onPostExecute(String result){
progress.dismiss();
delegate.processFinish(result);
}
} }
in login activity using the data i get from onPOstexecute method i move to another activity,here i handle the data,i have already created an interface for asyncresponse 在登录活动中,使用我从onPOstexecute方法获取的数据,我移至另一个活动,在这里我处理数据,我已经为asyncresponse创建了一个接口
public void processFinish(String output) {
// TODO Auto-generated method stub
status.setText(output);
if(output.equals(usname.getText().toString())){
Intent in=new Intent(Login.this,MainActivity.class);
startActivity(in);
}
else{
status.setText("Authentication Failed!!!");
}
}
Logcat: Logcat:
02-26 14:32:26.839: D/AndroidRuntime(5181): Shutting down VM
02-26 14:32:26.839: W/dalvikvm(5181): threadid=1: thread exiting with uncaught exception (group=0x4158bc80)
02-26 14:32:26.839: E/AndroidRuntime(5181): FATAL EXCEPTION: main
02-26 14:32:26.839: E/AndroidRuntime(5181): Process: com.testgcm, PID: 5181
02-26 14:32:26.839: E/AndroidRuntime(5181): java.lang.NullPointerException
02-26 14:32:26.839: E/AndroidRuntime(5181): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:81)
02-26 14:32:26.839: E/AndroidRuntime(5181): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:1)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.os.AsyncTask.finish(AsyncTask.java:632)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.os.Looper.loop(Looper.java:136)
02-26 14:32:26.839: E/AndroidRuntime(5181): at android.app.ActivityThread.main(ActivityThread.java:5081)
02-26 14:32:26.839: E/AndroidRuntime(5181): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 14:32:26.839: E/AndroidRuntime(5181): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 14:32:26.839: E/AndroidRuntime(5181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-26 14:32:26.839: E/AndroidRuntime(5181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 14:32:26.839: E/AndroidRuntime(5181): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-26 14:32:26.839: E/AndroidRuntime(5181): at dalvik.system.NativeStart.main(Native Method)
02-26 14:33:29.729: I/Process(5181): Sending signal. PID: 5181 SIG: 9
02-26 14:33:36.789: I/Adreno-EGL(5516): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
02-26 14:33:36.809: D/OpenGLRenderer(5516): Enabling debug mode 0
02-26 14:33:36.889: I/ActivityManager(5516): Timeline: Activity_idle id: android.os.BinderProxy@41d8fb18 time:25119895
02-26 14:33:45.209: D/AndroidRuntime(5516): Shutting down VM
02-26 14:33:45.209: W/dalvikvm(5516): threadid=1: thread exiting with uncaught exception (group=0x4158bc80)
02-26 14:33:45.219: E/AndroidRuntime(5516): FATAL EXCEPTION: main
02-26 14:33:45.219: E/AndroidRuntime(5516): Process: com.testgcm, PID: 5516
02-26 14:33:45.219: E/AndroidRuntime(5516): java.lang.NullPointerException
02-26 14:33:45.219: E/AndroidRuntime(5516): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:81)
02-26 14:33:45.219: E/AndroidRuntime(5516): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:1)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.os.AsyncTask.finish(AsyncTask.java:632)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.os.Looper.loop(Looper.java:136)
02-26 14:33:45.219: E/AndroidRuntime(5516): at android.app.ActivityThread.main(ActivityThread.java:5081)
02-26 14:33:45.219: E/AndroidRuntime(5516): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 14:33:45.219: E/AndroidRuntime(5516): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 14:33:45.219: E/AndroidRuntime(5516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-26 14:33:45.219: E/AndroidRuntime(5516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 14:33:45.219: E/AndroidRuntime(5516): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-26 14:33:45.219: E/AndroidRuntime(5516): at dalvik.system.NativeStart.main(Native Method)
02-26 14:33:46.799: I/Process(5516): Sending signal. PID: 5516 SIG: 9
02-26 14:33:49.979: I/Adreno-EGL(5648): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
02-26 14:33:50.019: D/OpenGLRenderer(5648): Enabling debug mode 0
02-26 14:33:50.069: I/ActivityManager(5648): Timeline: Activity_idle id: android.os.BinderProxy@41d85f08 time:25133075
02-26 14:33:58.499: D/AndroidRuntime(5648): Shutting down VM
02-26 14:33:58.499: W/dalvikvm(5648): threadid=1: thread exiting with uncaught exception (group=0x4158bc80)
02-26 14:33:58.509: E/AndroidRuntime(5648): FATAL EXCEPTION: main
02-26 14:33:58.509: E/AndroidRuntime(5648): Process: com.testgcm, PID: 5648
02-26 14:33:58.509: E/AndroidRuntime(5648): java.lang.NullPointerException
02-26 14:33:58.509: E/AndroidRuntime(5648): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:81)
02-26 14:33:58.509: E/AndroidRuntime(5648): at com.testgcm.SigninActivity.onPostExecute(SigninActivity.java:1)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.os.AsyncTask.finish(AsyncTask.java:632)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.os.Looper.loop(Looper.java:136)
02-26 14:33:58.509: E/AndroidRuntime(5648): at android.app.ActivityThread.main(ActivityThread.java:5081)
02-26 14:33:58.509: E/AndroidRuntime(5648): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 14:33:58.509: E/AndroidRuntime(5648): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 14:33:58.509: E/AndroidRuntime(5648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-26 14:33:58.509: E/AndroidRuntime(5648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 14:33:58.509: E/AndroidRuntime(5648): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-26 14:33:58.509: E/AndroidRuntime(5648): at dalvik.system.NativeStart.main(Native Method)
02-26 14:34:00.719: I/Process(5648): Sending signal. PID: 5648 SIG: 9
Even if i try to jump from AsyncTask
to another Activity
same error occurs, i am able to get the data from the server but its not getting stored in result variable of onPostExecute
method, Any help will be appreciated,thank you 即使我尝试从
AsyncTask
跳到另一个Activity
也会发生相同的错误,我也能够从服务器获取数据,但未将其存储在onPostExecute
方法的result变量中, onPostExecute
将不胜感激,谢谢
This line is your problem 这行是你的问题
delegate.processFinish(result);
your delegate
object is null. 您的
delegate
对象为空。 You declare it and never assign an instance to it. 您声明它,并且永远不会为其分配实例。
Error here delegate.processFinish(result);
错误在这里
delegate.processFinish(result);
need to assign instance for delegate
. 需要为
delegate
分配实例。
i've found that your delegate
variable newer initialized. 我发现您的
delegate
变量较新初始化。 Initialize that before You enter to onPostExecute
method. 在输入
onPostExecute
方法之前,先对其进行初始化。
In your code you have : 在您的代码中,您有:
public AsyncResponse delegate=null;
and later on 后来
delegate.processFinish(result);
What did you expect to happen? 您期望发生什么?
delegate is null
代表为
null
When you are performing operation( delegate.processFinish(result);
) on null delegate, that point is resulting in NullpointerException
. 当您对null委托执行操作(
delegate.processFinish(result);
NullpointerException
delegate.processFinish(result);
)时,该点将导致NullpointerException
。
即使在传递值之后,它也会显示异常“只有创建视图层次结构的原始线程才能触摸其视图”。因此,我只是在异步任务本身中操纵了数据,而不是将值传递回活动。感谢大家的回答我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.