NullPointerException onPostExecute()

时间:2017-02-15 18:33:23

标签: java php android

一旦我尝试启动应用,我就会收到此消息 不幸的是,你的应用已停止..似乎问题出在onPostExecute()方法!!

 import android.app.AlertDialog;
 import android.content.Context;
 import android.os.AsyncTask;
 import android.widget.Toast;

 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.ProtocolException;
 import java.net.URL;
 import java.net.URLEncoder;

 import static android.R.attr.type;

  /**
  * Created by star1 on 06/02/17.
  */

 public class BackgroundWorker extends AsyncTask<String, Void, String> {
 Context ctx;
  AlertDialog alertDialog;
//constructor
BackgroundWorker(Context ctx){
    this.ctx=ctx;
}

@Override
protected String doInBackground(String... params) {
    String reg_url = "http://10.0.3.2/MySqlDemo/reg.php";
    String login_url = "http://10.0.3.2/MySqlDemo/login.php";
    String method = params[0];
    if(method.equals("register")){


            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];
        try {
            URL url = new URL (reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+ "&"
                    +URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+ "&"
                    +URLEncoder.encode("user_pass", "UTF-8")+"="+URLEncoder.encode(user_pass, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();

            return "registeration success";


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    else if(method.equals("login")){
        String login_name = params[1];
        String login_pass = params[2];

        try {
            URL url = new URL (login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

            String data = URLEncoder.encode("login_name", "UTF-8")+"="+URLEncoder.encode(login_name, "UTF-8")+ "&"
                    +URLEncoder.encode("login_pass", "UTF-8")+"="+URLEncoder.encode(login_pass, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String response = "";
            String line = "";
            while((line = bufferedReader.readLine())!=null){
                response+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return response;


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    return null;
}

@Override
 protected void onPreExecute(){
 alertDialog = new AlertDialog.Builder(ctx).create();
 alertDialog.setTitle("login information");

}


@Override
protected void onPostExecute(String result)
{

        if (result.equals("registeration success")) {
            Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }


@Override
protected void onProgressUpdate(Void... values){
    super.onProgressUpdate(values);
}



 }

这是logcat文件,显示NullPointerException:

02-15 17:59:21.307 13981-13981/alsaad.layla.mysqldemo E/AndroidRuntime:            FATAL EXCEPTION: main
                                                                              java.lang.NullPointerException
                                                                        at alsaad.layla.mysqldemo.BackgroundWorker.onPostExecute(BackgroundWorker.java:133)
                                                                        at alsaad.layla.mysqldemo.BackgroundWorker.onPostExecute(BackgroundWorker.java:27)
                                                                        at android.os.AsyncTask.finish(AsyncTask.java:631)
                                                                        at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您需要检查resultnull是否onPostExecute

@Override
protected void onPostExecute(String result) {
    if (result == null) {
        Toast.makeText(ctx,"result was null", Toast.LENGTH_SHORT).show();
        return;
    }

    if (result.equals("registeration success")) {
        Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
    } else {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}