在执行doInBackground时出错()。致命异常:AsyncTask的#10 [英] An error occured while executing doInBackground(). FATAL EXCEPTION:AsyncTask #10

查看:2729
本文介绍了在执行doInBackground时出错()。致命异常:AsyncTask的#10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在click事件通过 PHP 本地主机上添加数据的MySQL 数据库。其表现出对单击事件以下logcat的错误。请帮我解决这个问题。

 七月8号至11日:31:06.416:D / gralloc_goldfish(971):仿真器没有检测GPU的仿真。
7月八日日至11日:31:53.377:D /内部点击(971):AAAAAAA
7月八日日至11日:31:53.837:D / INID doinbackground(971):AAAAAAA
7月八日日至11日:31:53.927:W / dalvikvm(971):主题ID = 11:线程退出与未捕获的异常(组= 0x409961f8)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):致命异常:AsyncTask的#1
7月八日日至11日:31:54.047:E / AndroidRuntime(971):java.lang.RuntimeException的:一个错误而执行doInBackground发生()
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在android.os.AsyncTask $ 3.done(AsyncTask.java:278)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:208)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.lang.Thread.run(Thread.java:856)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):由:显示java.lang.NullPointerException
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在com.example.connectiondemo.MainActivity $ CreateBooking.doInBackground(MainActivity.java:1)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
7月八日日至11日:31:54.047:E / AndroidRuntime(971):... 5个
7月八日日至11日:31:58.986:I /处理(971):发送信号。 PID:971 SIG:9
 

我的活动

 进口java.io.IOException异常;
进口java.io.UnsupportedEncodingException;
进口的java.util.ArrayList;
进口的java.util.List;

进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;

进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;

进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONException;
进口org.json.JSONObject;

进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;

进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;
进口android.widget.Toast;



公共类MainActivity延伸活动{
    私有静态最后字符串URL =htt​​p://10.0.2.2/styletapphp/create_booking.php;
    私有静态最后弦乐TAG_SUCCESS =成功;
    JSONParser jsonParser;
    的JSONObject JSON;

    TextView的T1,T2,T3,T4,T5,T6,T7,T8,T9;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        T1 =(TextView中)findViewById(R.id.t1);
        T2 =(TextView中)findViewById(R.id.t2);
        T3 =(TextView中)findViewById(R.id.t3);
        T4 =(TextView中)findViewById(R.id.t4);
        T5 =(TextView中)findViewById(R.id.t5);
        T6 =(TextView中)findViewById(R.id.t6);
        T7 =(TextView中)findViewById(R.id.t7);
        T8 =(TextView中)findViewById(R.id.t8);
        // T9 =(TextView中)findViewById(R.id.t8);
        t1.setText(日期);
        t2.setText(艺术家);
        t3.setText(9to10);
        t4.setText(10to11);
        t5.setText(11to12);
        t6.setText(12to1);
        t7.setText(2to3的);
        t8.setText(3to4);

        按钮butt1 =(按钮)findViewById(R.id.b1);
        butt1.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                Log.d(里面点击,AAAAAAA);
                //新CreateBooking()执行()。
                新CreateBooking()的执行(日,艺术家,9to10,10to11,11to12,12to1,2to3的,3to4);
            }
        });
    }
 类CreateBooking扩展的AsyncTask<字符串,整数,字符串> {

        @覆盖
        保护字符串doInBackground(字符串... PARAMS){

             Log.d(INID doinbackground,AAAAAAA);
             名单<的NameValuePair>的NameValuePair =新的ArrayList<的NameValuePair>();
                namevaluepair.add(新BasicNameValuePair(bookingdate,则params [0]));
                namevaluepair.add(新BasicNameValuePair(造型师,则params [1]));
                namevaluepair.add(新BasicNameValuePair(status9to10,则params [2]));
                namevaluepair.add(新BasicNameValuePair(status10to11,则params [3]));
                namevaluepair.add(新BasicNameValuePair(status11to12,则params [4]));
                namevaluepair.add(新BasicNameValuePair(status12to1,则params [5]));
                namevaluepair.add(新BasicNameValuePair(status2to3,则params [6]));
                namevaluepair.add(新BasicNameValuePair(status3to4,则params [7]));
                / *列表与LT;的NameValuePair>的NameValuePair =新的ArrayList<的NameValuePair>();
                namevaluepair.add(新BasicNameValuePair(bookingdate,t1.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(师,t2.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status9to10,t3.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status10to11,t4.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status11to12,t5.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status12to1,t6.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status2to3,t7.getText()的toString())。);
                namevaluepair.add(新BasicNameValuePair(status3to4,t8.getText()的toString())); * /

                 JSON = jsonParser.makehtt prequest(URL,POST的NameValuePair);

                        Log.d(创建订票反应,json.toString());

                //检查成功标签
                尝试{INT成功= json.getInt(TAG_SUCCESS);
                publishProgress(成功);

                }赶上(JSONException E){
                    e.printStackTrace();
                }
            返回null;
        }
        / *保护无效onPostExecute(整数整数){
            super.onPostExecute(整数);
            如果(整数== NULL)
                返回;
            如果(integer.intValue()== 1){
                Toast.makeText(getApplicationContext(),预订全成,Toast.LENGTH_LONG).show();
            } 其他 {
                Toast.makeText(getApplicationContext(),客人预订不是全成,Toast.LENGTH_LONG).show();
            }
        }
        保护无效onPostExecute(int i)以
        {
           super.onPostExecute(ⅰ);

            如果(ⅰ== 1){
                //创建成功产品
                Toast.makeText(getApplicationContext(),预订全成,Toast.LENGTH_LONG).show();
            }其他{
                 //创建失败产品
                Toast.makeText(getApplicationContext(),客人预订不是全成,Toast.LENGTH_LONG).show();
            }




           // if条件和烤面包的消息

        } * /
        保护无效onProgressUpdate(整数...值){
            super.onProgressUpdate(值);

            如果(值[0] == NULL)
                返回;
            如果(值[0] .intValue()== 1){
                Toast.makeText(getApplicationContext(),预订全成,Toast.LENGTH_LONG).show();
            } 其他 {
                Toast.makeText(getApplicationContext(),客人预订不是全成,Toast.LENGTH_LONG).show();
            }
        }
 

解决方案

jsonParser 从未被初始化。需要初始化,然后才能使用它。

另外你不能在 UIThread 网​​络请求。这将导致 NetworkOnMainThreadException 。您将需要使用的AsyncTask 。勾选此<一个href="http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception">NetworkOnMainThreadException

On the click event add data to MySQL database through php on the localhost. Its showing following logcat error on the click event. Please help me to solve this problem.

08-11 07:31:06.416: D/gralloc_goldfish(971): Emulator without GPU emulation detected.
08-11 07:31:53.377: D/Inside Click(971): aaaaaaa
08-11 07:31:53.837: D/inid doinbackground(971): aaaaaaa
08-11 07:31:53.927: W/dalvikvm(971): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
08-11 07:31:54.047: E/AndroidRuntime(971): FATAL EXCEPTION: AsyncTask #1
08-11 07:31:54.047: E/AndroidRuntime(971): java.lang.RuntimeException: An error occured while executing doInBackground()
08-11 07:31:54.047: E/AndroidRuntime(971):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-11 07:31:54.047: E/AndroidRuntime(971):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.lang.Thread.run(Thread.java:856)
08-11 07:31:54.047: E/AndroidRuntime(971): Caused by: java.lang.NullPointerException
08-11 07:31:54.047: E/AndroidRuntime(971):  at com.example.connectiondemo.MainActivity$CreateBooking.doInBackground(MainActivity.java:1)
08-11 07:31:54.047: E/AndroidRuntime(971):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-11 07:31:54.047: E/AndroidRuntime(971):  ... 5 more
08-11 07:31:58.986: I/Process(971): Sending signal. PID: 971 SIG: 9

My Activity

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity {
    private static final String url="http://10.0.2.2/styletapphp/create_booking.php";
    private static final String TAG_SUCCESS="Success";
    JSONParser jsonParser;
    JSONObject json;

    TextView t1,t2,t3,t4,t5,t6,t7,t8,t9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=(TextView) findViewById(R.id.t1);
        t2=(TextView) findViewById(R.id.t2);
        t3=(TextView) findViewById(R.id.t3);
        t4=(TextView) findViewById(R.id.t4);
        t5=(TextView) findViewById(R.id.t5);
        t6=(TextView) findViewById(R.id.t6);
        t7=(TextView) findViewById(R.id.t7);
        t8=(TextView) findViewById(R.id.t8);
        //t9=(TextView) findViewById(R.id.t8);
        t1.setText("Date");
        t2.setText("Stylist");
        t3.setText("9to10");
        t4.setText("10to11");
        t5.setText("11to12");
        t6.setText("12to1");
        t7.setText("2to3");
        t8.setText("3to4");

        Button butt1= (Button) findViewById(R.id.b1);
        butt1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("Inside Click","aaaaaaa");
                //new CreateBooking().execute();
                new CreateBooking().execute("Date","Stylist", "9to10", "10to11", "11to12", "12to1","2to3", "3to4"); 
            }
        });
    }
 class CreateBooking extends AsyncTask<String,Integer,String>{

        @Override
        protected String doInBackground(String... params) {

             Log.d("inid doinbackground","aaaaaaa");
             List<NameValuePair> namevaluepair = new ArrayList<NameValuePair>();
                namevaluepair.add(new BasicNameValuePair("bookingdate", params[0]));
                namevaluepair.add(new BasicNameValuePair("stylist", params[1]));
                namevaluepair.add(new BasicNameValuePair("status9to10", params[2]));
                namevaluepair.add(new BasicNameValuePair("status10to11", params[3]));
                namevaluepair.add(new BasicNameValuePair("status11to12", params[4]));
                namevaluepair.add(new BasicNameValuePair("status12to1", params[5]));
                namevaluepair.add(new BasicNameValuePair("status2to3", params[6]));
                namevaluepair.add(new BasicNameValuePair("status3to4", params[7]));
                /*List<NameValuePair>namevaluepair=new ArrayList<NameValuePair>();
                namevaluepair.add(new BasicNameValuePair("bookingdate",t1.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("stylist",t2.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status9to10",t3.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status10to11",t4.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status11to12",t5.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status12to1",t6.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status2to3",t7.getText().toString()));
                namevaluepair.add(new BasicNameValuePair("status3to4",t8.getText().toString()));*/

                 json=jsonParser.makehttpRequest(url,"POST",namevaluepair);

                        Log.d("Create booking response",json.toString());

                // check for success tag
                try{int success=json.getInt(TAG_SUCCESS);
                publishProgress(success);

                }catch(JSONException e){
                    e.printStackTrace();
                }
            return null;
        }
        /*protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            if (integer == null)
                return;
            if (integer.intValue() == 1) {
                Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
            }
        }
        protected void onPostExecute(int i)
        {
           super.onPostExecute(i);

            if (i==1){
                // successfully created product
                Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
            }else{
                 // failed to create product
                Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
            }




           // if condition and toast message

        } */
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

            if (values[0] == null)
                return;
            if (values[0].intValue() == 1) {
                Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
            }
        }

解决方案

jsonParser has never been initialized. You need to initialize before you can use it.

Also you can not make Network requests on the UIThread. This will cause a NetworkOnMainThreadException. You will need to use an AsyncTask. Check this NetworkOnMainThreadException

这篇关于在执行doInBackground时出错()。致命异常:AsyncTask的#10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆