2017-02-15 9 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); 
} 



} 

は、NullPointerExceptionが示しlogcatファイルです:

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

nullではないことを確認する必要があります() '他のステップに失敗した場合私はあなたのサーバー側のファイルが正しく動作しているかどうかを確認します。 – Pztar

+0

横スクロールを必要としないようにログをフォーマットできますか? – Gustave

答えて

0

あなたがresultはあなたがあなたの `doInBackgroundにヌルを返している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(); 
    } 
} 
+0

私はそれを試みたが、まだヌル値が与えている!! – layla7

+0

それはあなたに戻ってnull値を返すだ場合、あなたは別の問題を持っている、とdoInBackground' –

+0

'で、さらに調査する必要があります。あなたは問題が何であるかを伝えることはできますか? – layla7