执行Android应用程序时出现致命错误

时间:2017-04-27 16:56:42

标签: android android-asynctask

为什么应用程序不幸被停止?

错误是:

 java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:300)
 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
 at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NullPointerException
 at com.example.prateek.urbanityc.Bg.doInBackground(Bg.java:45)
 at com.example.prateek.urbanityc.Bg.doInBackground(Bg.java:24)
 at android.os.AsyncTask$2.call(AsyncTask.java:288)
 at java.util.concurrent.FutureTask.run(FutureTask.java:237)

android代码是: -

.java文件

   package com.example.prateek.urbanityc;

   import android.content.DialogInterface;
   import android.content.Intent;
   import android.support.v7.app.AlertDialog;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;
   import android.widget.RadioButton;
   import android.widget.RadioGroup;
   import android.widget.TextView;
   import android.widget.Toast;

   public class choose_ngo extends AppCompatActivity implements 
    AsyncResponse {
    Button next, skip;
    TextView tv8;
    //String b;
    //GlobalVariable gb;
    String email, type1, type2;

RadioGroup rg;
String a,b;
GlobalVariable gb;
int selectedid;
RadioButton rbvalue;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_ngo);

    /*rg=(RadioGroup) findViewById(R.id.radiogroup);
    selectedid = rg.getCheckedRadioButtonId();
    rbvalue = (RadioButton) findViewById(selectedid);
    a=rbvalue.getText().toString();  */

    skip = (Button) findViewById(R.id.button2);
    next = (Button) findViewById(R.id.button6);
   // tv8 = (TextView) findViewById(R.id.textView8);
    gb = GlobalVariable.getSingleInstance();

}

public void verify(View view) {

    type1 = "store_ngo";
    type2 = "show_details";
    email = gb.getVariables().get("email");

    //Toast.makeText(this, email, Toast.LENGTH_SHORT).show();
    /*Toast.makeText(this, a, Toast.LENGTH_SHORT).show();

    Bg backgroundWorker = new BackgroundWorker(this, this);
    backgroundWorker.delegate = this;
    backgroundWorker.execute(type1, email,a);
     */

    back();



        }

public void processFinish(String output) {


    AlertDialog.Builder builder = new AlertDialog.Builder(choose_ngo.this);
    builder.setTitle("Verify Details");
    builder.setMessage(output);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivity(new Intent(getApplicationContext(), thanku.class));
            finish();

        }
    });
    builder.setNegativeButton("Edit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivity(new Intent(getApplicationContext(), verify_details.class));
            finish();
        }
    });

    builder.show();
}

public void back()
{
    Bg Bg = new Bg(this, this);
    Bg.delegate = this;
    Bg.execute(type2, email);
}

public void skip(View view) {
    email = gb.getVariables().get("email");
    back();


}

public void store() {
    b = getIntent().getExtras().getString("email2");
}
 }

backgroundworker类的代码

  package com.example.prateek.urbanityc;

  import android.app.AlertDialog;
  import android.app.ProgressDialog;
  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.URL;
 import java.net.URLEncoder;

 public class Bg extends AsyncTask<String,Void,String> {

Context context;
AlertDialog alertDialog;

AsyncResponse delegate;

Bg(Context ctx, AsyncResponse delegate) {
    context = ctx;
    this.delegate = delegate;
}

@Override
protected String doInBackground(String... params) {
    String type = params[0];

    String item_info_url = "http://urbanity.000webhostapp.com/item_typ.php";
    String show_info_url = "http://urbanity.000webhostapp.com/verify.php";



   if(type.equals("item_info")) {

        try {

            String email = params[1];
            String a = params[2];
            String b = params[3];
            String c = params[4];

            URL url = new URL(item_info_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 post_data =
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                            URLEncoder.encode("a", "UTF-8") + "=" + URLEncoder.encode(a, "UTF-8") + "&" +
                            URLEncoder.encode("b", "UTF-8") + "=" + URLEncoder.encode(b, "UTF-8") + "&" +
                            URLEncoder.encode("c", "UTF-8") + "=" + URLEncoder.encode(c, "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result = "";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else if(type.equals("show_details")) {

        try {

            String email = params[1];

            URL url = new URL(show_info_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 post_data =
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");

            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result = "";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}


@Override

protected void onPostExecute(String result) {

    alertDialog.setMessage(result);
    if(result!=null){
        delegate.processFinish(result); }
    //alertDialog.show();

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

.xml文件

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_choose_ngo"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
        android:paddingBottom="16dp"
     android:paddingLeft="64dp"
     android:background="@drawable/giving"
     android:paddingRight="64dp"
     android:paddingTop="16dp"
     tools:context="com.example.prateek.urbanityc.choose_ngo">

    <Button
     android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radiogroup"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:layout_alignParentLeft="true"
    android:text="SKIP"
    android:textStyle="bold"
    android:onClick="skip"
    />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp"
    android:text="NGOs AVAILABLE "
    android:textSize="25dp"

    android:textStyle="bold"
    android:textColor="#0000FF"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true" />
    <RadioGroup
    android:layout_width="wrap_content"
    android:id="@+id/radiogroup"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_below="@+id/textView3">


    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:textSize="20dp"
        android:text="Gramiksha"
        android:layout_below="@+id/textView4"
        />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="20dp"
        android:text="Pryatna"
        android:layout_below="@+id/radioButton"
        />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AshaKiran"
        android:layout_marginTop="12dp"
        android:textSize="20dp"
        android:layout_below="@+id/radioButton2"
        />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Udan"
        android:textSize="20dp"
        android:layout_below="@+id/radioButton3"
        />
</RadioGroup>

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT"
    android:textStyle="bold"
    android:onClick="verify"
    android:layout_alignTop="@+id/button2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<!-- <TextView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:text="Skip"
    android:textSize="20dp"
    android:textStyle="bold"
    android:layout_alignBottom="@+id/textView4"
    android:layout_marginBottom="22dp"
    android:id="@+id/textView8"
    android:layout_alignParentTop="true"
    android:onClick="skip"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />  -->

   </RelativeLayout>

php文件

   <?php
   require "con1.php";

   $email=$_POST["email"];
    $contact='';
  $add='';
   //$c=$_POST["c"]; 

   require_once('con1.php') ;

   $sql = "SELECT u_contact,u_add FROM user where u_mail = '$email' ";

   if ($result=mysqli_query($conn,$sql))
       {
     // Fetch one and one row
    while ($row=mysqli_fetch_row($result))
      {
         echo "Contact: ".$row[0];
         echo "                           ";
        echo "Address: ".$row[1];
        }
     }

    // mysqli_close($conn);

     ?>

应用程序停止点击跳过按钮,而应用程序中按钮的所有其他点击都会运行。我无法理解为什么会发生这种情况我已经做了很多谷歌来纠正这个问题,但我无法找到解决方案。所以,plzz帮助我们......

2 个答案:

答案 0 :(得分:0)

您在AsyncTask中获得nullPointerException。你能检查一下你是否发送非空类型。

if(type.equals("item_info"))

应该是

if(type!= null && type.equals("item_info"))

答案 1 :(得分:0)

我认为您正在运行多个异步任务,这就是您收到此错误的原因。像这样运行你的异步任务

AsyncTaskCompat.executeParallel(new YourAsyncTask());

如果这解决了您的问题,请告诉我。