-8

可能重复:
AsyncTask 和 Looper.prepare() 错误

10-03 18:38:32.041: E/Trace(685): error opening trace file: No such file or directory (2)
10-03 18:39:30.731: E/AndroidRuntime(685): FATAL EXCEPTION: AsyncTask #1
10-03 18:39:30.731: E/AndroidRuntime(685): java.lang.RuntimeException: An error occured while executing doInBackground()
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.lang.Thread.run(Thread.java:856)
10-03 18:39:30.731: E/AndroidRuntime(685): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.os.Handler.<init>(Handler.java:121)
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.widget.Toast$TN.<init>(Toast.java:322)
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.widget.Toast.<init>(Toast.java:91)
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.widget.Toast.makeText(Toast.java:238)
10-03 18:39:30.731: E/AndroidRuntime(685):  at com.example.annapurna.DisplayContri$CreateContribution.doInBackground(DisplayContri.java:95)
10-03 18:39:30.731: E/AndroidRuntime(685):  at com.example.annapurna.DisplayContri$CreateContribution.doInBackground(DisplayContri.java:1)
10-03 18:39:30.731: E/AndroidRuntime(685):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-03 18:39:30.731: E/AndroidRuntime(685):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-03 18:39:30.731: E/AndroidRuntime(685):  ... 5 more
10-03 18:39:32.461: E/WindowManager(685): Activity com.example.annapurna.DisplayContri has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4100b318 that was originally added here
10-03 18:39:32.461: E/WindowManager(685): android.view.WindowLeaked: Activity com.example.annapurna.DisplayContri has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4100b318 that was originally added here
10-03 18:39:32.461: E/WindowManager(685):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.Window$LocalWindowManager.addView(Window.java:547)
10-03 18:39:32.461: E/WindowManager(685):   at android.app.Dialog.show(Dialog.java:277)
10-03 18:39:32.461: E/WindowManager(685):   at com.example.annapurna.DisplayContri$CreateContribution.onPreExecute(DisplayContri.java:82)
10-03 18:39:32.461: E/WindowManager(685):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-03 18:39:32.461: E/WindowManager(685):   at android.os.AsyncTask.execute(AsyncTask.java:534)
10-03 18:39:32.461: E/WindowManager(685):   at com.example.annapurna.DisplayContri$1.onClick(DisplayContri.java:60)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.View.performClick(View.java:4084)
10-03 18:39:32.461: E/WindowManager(685):   at android.view.View$PerformClick.run(View.java:16966)
10-03 18:39:32.461: E/WindowManager(685):   at android.os.Handler.handleCallback(Handler.java:615)
10-03 18:39:32.461: E/WindowManager(685):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 18:39:32.461: E/WindowManager(685):   at android.os.Looper.loop(Looper.java:137)
10-03 18:39:32.461: E/WindowManager(685):   at android.app.ActivityThread.main(ActivityThread.java:4745)
10-03 18:39:32.461: E/WindowManager(685):   at java.lang.reflect.Method.invokeNative(Native Method)
10-03 18:39:32.461: E/WindowManager(685):   at java.lang.reflect.Method.invoke(Method.java:511)
10-03 18:39:32.461: E/WindowManager(685):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-03 18:39:32.461: E/WindowManager(685):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-03 18:39:32.461: E/WindowManager(685):   at dalvik.system.NativeStart.main(Native Method)
10-03 18:44:31.601: E/Trace(712): error opening trace file: No such file or directory (2)
10-03 18:50:53.220: E/AndroidRuntime(712): FATAL EXCEPTION: AsyncTask #1
10-03 18:50:53.220: E/AndroidRuntime(712): java.lang.RuntimeException: An error occured while executing doInBackground()
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.lang.Thread.run(Thread.java:856)
10-03 18:50:53.220: E/AndroidRuntime(712): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.os.Handler.<init>(Handler.java:121)
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.widget.Toast$TN.<init>(Toast.java:322)
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.widget.Toast.<init>(Toast.java:91)
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.widget.Toast.makeText(Toast.java:238)
10-03 18:50:53.220: E/AndroidRuntime(712):  at com.example.annapurna.DisplayContri$CreateContribution.doInBackground(DisplayContri.java:95)
10-03 18:50:53.220: E/AndroidRuntime(712):  at com.example.annapurna.DisplayContri$CreateContribution.doInBackground(DisplayContri.java:1)
10-03 18:50:53.220: E/AndroidRuntime(712):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-03 18:50:53.220: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-03 18:50:53.220: E/AndroidRuntime(712):  ... 5 more
10-03 18:50:54.580: E/WindowManager(712): Activity com.example.annapurna.DisplayContri has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4100a568 that was originally added here
10-03 18:50:54.580: E/WindowManager(712): android.view.WindowLeaked: Activity com.example.annapurna.DisplayContri has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4100a568 that was originally added here
10-03 18:50:54.580: E/WindowManager(712):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.Window$LocalWindowManager.addView(Window.java:547)
10-03 18:50:54.580: E/WindowManager(712):   at android.app.Dialog.show(Dialog.java:277)
10-03 18:50:54.580: E/WindowManager(712):   at com.example.annapurna.DisplayContri$CreateContribution.onPreExecute(DisplayContri.java:82)
10-03 18:50:54.580: E/WindowManager(712):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-03 18:50:54.580: E/WindowManager(712):   at android.os.AsyncTask.execute(AsyncTask.java:534)
10-03 18:50:54.580: E/WindowManager(712):   at com.example.annapurna.DisplayContri$1.onClick(DisplayContri.java:60)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.View.performClick(View.java:4084)
10-03 18:50:54.580: E/WindowManager(712):   at android.view.View$PerformClick.run(View.java:16966)
10-03 18:50:54.580: E/WindowManager(712):   at android.os.Handler.handleCallback(Handler.java:615)
10-03 18:50:54.580: E/WindowManager(712):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 18:50:54.580: E/WindowManager(712):   at android.os.Looper.loop(Looper.java:137)
10-03 18:50:54.580: E/WindowManager(712):   at android.app.ActivityThread.main(ActivityThread.java:4745)
10-03 18:50:54.580: E/WindowManager(712):   at java.lang.reflect.Method.invokeNative(Native Method)
10-03 18:50:54.580: E/WindowManager(712):   at java.lang.reflect.Method.invoke(Method.java:511)
10-03 18:50:54.580: E/WindowManager(712):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-03 18:50:54.580: E/WindowManager(712):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-03 18:50:54.580: E/WindowManager(712):   at dalvik.system.NativeStart.main(Native Method)
10-03 18:50:57.681: E/Trace(732): error opening trace file: No such file or directory (2)

这里给出了发生错误的程序代码 -

 package com.example.annapurna;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DisplayContri extends Activity {
    // Progress Dialog
        private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();
        EditText editText1;
        //EditText  text2;
       //EditText  text3;

        // url to create new product
        private static String url_create_product = "http://10.0.2.2/annapurna_connect/userold.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_contri);


        //Edit text
        editText1 = (EditText) findViewById(R.id.editText1);
        //text2 = (EditText) findViewById(R.id.editText2);
        //text3 = (EditText) findViewById(R.id.editText3);

        //Create Button
        Button button1 = (Button) findViewById(R.id.button1);

        //button click event
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Creating new Contribution entry in background thread
                new CreateContribution().execute("");

            }
        });
    }


    /**
     * Background Async Task to Create new product
     * */
    class CreateContribution extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DisplayContri.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) 
        {

            String name = editText1.getText().toString();
            //String age = (text2.getText().toString());
            //String id = (text3.getText().toString());
            Toast toast2=Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_LONG);//This is where the error shows  
             toast2.show(); 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            //params.add(new BasicNameValuePair("age", age));
            //params.add(new BasicNameValuePair("id", id));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

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

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), Annapurna.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_display_contri, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void contributor(View view) {
        Intent intent = new Intent(this, Contributor.class);

        startActivity(intent);
    }

   }

有一个用于连接的 Jsonparsor.java 类。在上面的代码中,doInBackground方法()没有执行,我通过添加toast测试了应用程序,我发现它没有进入doInBackground方法并执行到preexecute()的最后结束

4

2 回答 2

0

您正在尝试从 AsyncTask 的 doInBackground() 更新 UI。

这是不可能的。

如果您有任何 Toast 或您尝试从 doInBackground() 到 UI 的任何其他更新,请确保删除。

于 2012-10-03T13:29:05.557 回答
0

检查您是否在主线程以外的线程上创建 AsyncTask。

于 2012-10-03T13:27:54.667 回答