使用AsyncTask在视图中设置数据时获取致命异常

时间:2016-04-20 04:46:30

标签: android android-asynctask

以下是我的logcat

04-20 00:40:11.688: E/AndroidRuntime(1623): FATAL EXCEPTION: AsyncTask #1
    04-20 00:40:11.688: E/AndroidRuntime(1623): Process: com.clip.android, PID: 1623
    04-20 00:40:11.688: E/AndroidRuntime(1623): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.lang.Thread.run(Thread.java:841)
    04-20 00:40:11.688: E/AndroidRuntime(1623): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.os.Handler.<init>(Handler.java:200)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.os.Handler.<init>(Handler.java:114)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.widget.Toast$TN.<init>(Toast.java:327)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.widget.Toast.<init>(Toast.java:92)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.widget.Toast.makeText(Toast.java:241)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at com.clip.android.ClaimRegisterPage$AsyncCallWS.doInBackground(ClaimRegisterPage.java:124)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at com.clip.android.ClaimRegisterPage$AsyncCallWS.doInBackground(ClaimRegisterPage.java:1)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    04-20 00:40:11.688: E/AndroidRuntime(1623):     ... 4 more
    04-20 00:40:11.788: W/EGL_emulation(1623): eglSurfaceAttrib not implemented
    04-20 00:40:12.238: E/WindowManager(1623): android.view.WindowLeaked: Activity com.clip.android.ClaimRegisterPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{a56affd0 V.E..... R......D 0,0-571,339} that was originally added here
    04-20 00:40:12.238: E/WindowManager(1623):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.Dialog.show(Dialog.java:286)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ProgressDialog.show(ProgressDialog.java:99)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ProgressDialog.show(ProgressDialog.java:94)
    04-20 00:40:12.238: E/WindowManager(1623):  at com.clip.android.ClaimRegisterPage$AsyncCallWS.onPreExecute(ClaimRegisterPage.java:133)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.os.AsyncTask.execute(AsyncTask.java:535)
    04-20 00:40:12.238: E/WindowManager(1623):  at com.clip.android.ClaimRegisterPage.onCreate(ClaimRegisterPage.java:93)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.Activity.performCreate(Activity.java:5231)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.os.Handler.dispatchMessage(Handler.java:102)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.os.Looper.loop(Looper.java:136)
    04-20 00:40:12.238: E/WindowManager(1623):  at android.app.ActivityThread.main(ActivityThread.java:5001)
    04-20 00:40:12.238: E/WindowManager(1623):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-20 00:40:12.238: E/WindowManager(1623):  at java.lang.reflect.Method.invoke(Method.java:515)
    04-20 00:40:12.238: E/WindowManager(1623):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    04-20 00:40:12.238: E/WindowManager(1623):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    04-20 00:40:12.238: E/WindowManager(1623):  at dalvik.system.NativeStart.main(Native Method)

以下是我的代码

package com.clip.android;

import java.util.ArrayList;
import java.util.List;



import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class ClaimRegisterPage extends ClaimRegister {

    TextView custid;
    String customerid;

    TextView custname;
    String customername;

    TextView prdid;
    String productid;

    String clmtype;

    TextView prdname;
    String productname;

    TextView prdline;
    String productline;

    TextView pfrom;
    String prdfrom;

    TextView pto;
    String prdto;

    TextView sumins;
    String suminsured;

    String policyId;

    LinearLayout searchButtonListner;

    ProgressDialog progress;




    List<Policy> policys = new ArrayList<Policy>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeader("Policy Details", true, true);
        setContentView(R.layout.activity_claim_register_page);
        custid = (TextView) findViewById(R.id.pcustomerid);
        custname = (TextView) findViewById(R.id.pcustomername);
        prdid = (TextView) findViewById(R.id.pproductid);
        prdname = (TextView) findViewById(R.id.pproductname);
        prdline = (TextView) findViewById(R.id.pproductline);
        pfrom = (TextView) findViewById(R.id.pperiodfrom);
        pto = (TextView) findViewById(R.id.pperiodto);
        sumins = (TextView) findViewById(R.id.psuminsured);
    /*  searchButtonListner.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
            finish();
            }
        });
        */


        Intent intent = getIntent();
        if(intent!=null){
        policyId = intent.getStringExtra("userValue");
        clmtype = intent.getStringExtra("getclmType");
        Log.d("page", "1");

        AsyncCallWS task = new AsyncCallWS();
        task.execute();
        }



    }





      private class AsyncCallWS extends AsyncTask<String, Void, Void> {
            @SuppressLint("NewApi")
            @Override
            protected Void doInBackground(String... params) {


                policys = PolicyWebService.invokeHelloWorldWS(policyId, "getPolicyDetails");

                for(Policy policy:policys){
                    customerid =  policy.getID();
                    customername = policy.getFULLNAME();
                    productid =  policy.getPRODUCTID();
                    productname =  policy.getPRODUCTNAME();
                    productline =  policy.getPRODUCTLINE();
                    prdfrom =  policy.getPERIODFROM();
                    prdto =  policy.getPERIODTO();
                    suminsured= policy.getTOTALSI();
                }
                Log.d("Page", "Policys = " +policys.size());
                if(policys.size()==0){
                    Toast.makeText(ClaimRegisterPage.this, "NO records Found", Toast.LENGTH_LONG).show();
            }
                return null;
            }

            @Override
            protected void onPreExecute() {
                Log.d("page", "onpreExecute");

                progress = ProgressDialog.show(ClaimRegisterPage.this, "Retrieving Policy Details", "Please Wait.........!");

            }
            @Override
            protected void onPostExecute(Void result) {
                Log.d("page", "onpostExecute");
                if(policys.size()==0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(ClaimRegisterPage.this);
                    builder.setMessage( "Sorry No Record Found").setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                              finish();
                            } 
                        }); 
                    AlertDialog alert = builder.create();
                    alert.show();     
                }


                progress.dismiss();
                Log.d("page", "lolasdsad");
                custid.setText(customerid);
                custname.setText(customername);
                prdid.setText(productid);
                prdname.setText(productname);
                prdline.setText(productline);
                pfrom.setText(prdfrom);
                pto.setText(prdto);
                sumins.setText(suminsured);

            }   
      }
}

我得到了一些错误,因为新的android我无法追踪它。我收到的错误如上所述我发布了,我也发布了我的代码。任何人都可以帮我解决我的问题..我想我在asyncTask中有问题。它不允许我将数据设置为视图..

2 个答案:

答案 0 :(得分:2)

您正在doInBackground()中显示Toast。所有UI操作都应该在主线程中执行。

答案 1 :(得分:1)

您无法在Toast.makeText()内拨打doInBackground()

如果您想在doInbackground()执行操作时显示某些内容作为进度更新的一部分,则只需使用某些状态代码调用publishProgress(),然后使用onProgressUpdate()方法即可检查状态代码并相应地显示你想要的Toast。

使用如下:

private static final int NO_RECORD_ERROR = 1;
private class AsyncCallWS extends AsyncTask<String, Integer, Void> {
 protected Void doInBackground(URL... urls) {
     .....
     if(policys.size()==0){
      publishProgress(NO_RECORD_ERROR);           
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     int progressCode= progress[0];
if (progressCode == NO_RECORD_ERROR) {
Toast.makeText(ClaimRegisterPage.this, "NO records Found", Toast.LENGTH_LONG).show();
 }
}