2017-02-15 90 views
-2

我不斷收到此消息,一旦我嘗試啓動我的應用程序 不幸的是,您的應用程序已停止..似乎問題是在onPostExecute()方法!NullPointerException 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) 
+0

你在你的'doInBackground返回null ()'如果其他步驟失敗。我會檢查您的服務器端文件是否正常工作。 – Pztar

+0

你可以格式化你的日誌,以便它不需要水平滾動嗎? – Gustave

回答

0

您需要檢查resultnullonPostExecute

@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(); 
    } 
} 
+0

我試過了,但仍然是空值給! – layla7

+0

如果它返回一個空值給你,那麼你有一個單獨的問題,並需要進一步調查'doInBackground' –

+0

你能說出是什麼問題嗎? – layla7