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);

       }


}

在登录活动中,使用我从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:

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


即使我尝试从AsyncTask跳转到另一个Activity,也会发生相同的错误,
我能够从服务器获取数据,但未将其存储在onPostExecute方法的结果变量中,
任何帮助将不胜感激,谢谢

最佳答案

这行是你的问题

delegate.processFinish(result);


您的delegate对象为null。您声明它,并且永远不会为其分配实例。

10-05 22:52