简体   繁体   English

此应用程序可能在其主线程中做过多的工作

[英]This application may be doing too much work in its main thread

i am using the following piece of code to add an item to mysql database.When the button is clicked,it should begin the Async task that uploads data to the server.But when i run it i get the error message "This application may be doing too much work in its main thread" from LogCat.How can i fix this code problem,thanks. 我正在使用以下代码将项目添加到mysql数据库。单击按钮时,它应该开始将数据上传到服务器的异步任务。但是当我运行它时,我收到错误消息“此应用程序可能是“在LogCat的主线程中做了太多工作”。谢谢。我该如何解决此代码问题。

public class NewProductActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;

// url to create new product
private static String url_create_product = "http://localhost/android_connect/create_product.php";

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

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

    // Edit Text
    inputName = (EditText) findViewById(R.id.inputName);
    inputPrice = (EditText) findViewById(R.id.inputPrice);
    inputDesc = (EditText) findViewById(R.id.inputDesc);

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

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

        @Override
        public void onClick(View view) {
            // creating new product in background thread

            new CreateNewProduct().execute();

        }
    });
}

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

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

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String name = inputName.getText().toString();
        String price = inputPrice.getText().toString();
        String description = inputDesc.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("price", price));
        params.add(new BasicNameValuePair("description", description));

        // 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(), AllProductsActivity.class);
                startActivity(i);

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

        return null;
    }

this is the LogCat output: 这是LogCat的输出:

05-08 12:51:26.761: I/Choreographer(762): Skipped 31 frames!  The application may be doing too much work on its main thread.
05-08 12:51:28.632: W/System.err(762): org.apache.http.conn.HttpHostConnectException: "Connection to localhost refused
05-08 12:51:28.672: W/System.err(762):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-08 12:51:28.672: W/System.err(762):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-08 12:51:28.712: W/System.err(762):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-08 12:51:28.712: W/System.err(762):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-08 12:51:28.832: W/System.err(762):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-08 12:51:28.902: W/System.err(762):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-08 12:51:29.012: W/System.err(762):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-08 12:51:29.072: W/System.err(762):  at com.example.androidhive.JSONParser.makeHttpRequest(JSONParser.java:51)
05-08 12:51:29.072: W/System.err(762):  at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:97)
05-08 12:51:29.172: W/System.err(762):  at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:1)
05-08 12:51:29.252: W/System.err(762):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-08 12:51:29.312: W/System.err(762):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-08 12:51:29.322: W/System.err(762):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-08 12:51:29.342: W/System.err(762):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-08 12:51:29.342: W/System.err(762):  at java.lang.Thread.run(Thread.java:856)
05-08 12:51:29.372: W/System.err(762): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
05-08 12:51:29.442: W/System.err(762):  at libcore.io.IoBridge.connect(IoBridge.java:114)
05-08 12:51:29.493: W/System.err(762):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-08 12:51:29.552: W/System.err(762):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-08 12:51:29.552: W/System.err(762):  at java.net.Socket.connect(Socket.java:842)
05-08 12:51:29.572: W/System.err(762):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-08 12:51:29.652: W/System.err(762):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-08 12:51:29.692: W/System.err(762):  ... 14 more
05-08 12:51:29.763: W/System.err(762): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
05-08 12:51:29.872: W/System.err(762):  at libcore.io.Posix.connect(Native Method)
05-08 12:51:29.872: W/System.err(762):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-08 12:51:29.892: W/System.err(762):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-08 12:51:29.992: W/System.err(762):  at libcore.io.IoBridge.connect(IoBridge.java:112)
05-08 12:51:29.992: W/System.err(762):  ... 19 more
05-08 12:51:30.102: E/Buffer Error(762): Error converting result java.lang.NullPointerException: lock == null
05-08 12:51:30.212: E/JSON Parser(762): Error parsing data org.json.JSONException: End of input at character 0 of 
05-08 12:51:30.302: W/dalvikvm(762): threadid=14: thread exiting with uncaught exception (group=0x40a71930)
05-08 12:51:30.602: E/AndroidRuntime(762): FATAL EXCEPTION: AsyncTask #4
05-08 12:51:30.602: E/AndroidRuntime(762): java.lang.RuntimeException: An error occured while executing doInBackground()
05-08 12:51:30.602: E/AndroidRuntime(762):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.lang.Thread.run(Thread.java:856)
05-08 12:51:30.602: E/AndroidRuntime(762): Caused by: java.lang.NullPointerException
05-08 12:51:30.602: E/AndroidRuntime(762):  at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:101)    
05-08 12:51:30.602: E/AndroidRuntime(762):  at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:1)
05-08 12:51:30.602: E/AndroidRuntime(762):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-08 12:51:30.602: E/AndroidRuntime(762):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-08 12:51:30.602: E/AndroidRuntime(762):  ... 3 more
05-08 12:51:32.542: I/Choreographer(762): Skipped 395 frames!  The application may be doing too much work on its main thread.
05-08 12:51:32.752: I/Choreographer(762): Skipped 54 frames!  The application may be doing too much work on its main thread.
05-08 12:51:33.892: I/Choreographer(762): Skipped 295 frames!  The application may be doing too much work on its main thread.
05-08 12:51:34.132: I/Choreographer(762): Skipped 58 frames!  The application may be doing too much work on its main thread.
05-08 12:51:35.762: E/WindowManager(762): Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d3d5e0 V.E..... R.....ID 0,0-329,175} that was originally added here
05-08 12:51:35.762: E/WindowManager(762): android.view.WindowLeaked: Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d3d5e0 V.E..... R.....ID 0,0-329,175} that was originally added here
05-08 12:51:35.762: E/WindowManager(762):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
05-08 12:51:35.762: E/WindowManager(762):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
05-08 12:51:35.762: E/WindowManager(762):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-08 12:51:35.762: E/WindowManager(762):   at android.app.Dialog.show(Dialog.java:281)
05-08 12:51:35.762: E/WindowManager(762):   at com.example.androidhive.NewProductActivity$CreateNewProduct.onPreExecute(NewProductActivity.java:78)
05-08 12:51:35.762: E/WindowManager(762):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-08 12:51:35.762: E/WindowManager(762):   at android.os.AsyncTask.execute(AsyncTask.java:534)
05-08 12:51:35.762: E/WindowManager(762):   at com.example.androidhive.NewProductActivity$1.onClick(NewProductActivity.java:57)
05-08 12:51:35.762: E/WindowManager(762):   at android.view.View.performClick(View.java:4204)
05-08 12:51:35.762: E/WindowManager(762):   at android.view.View$PerformClick.run(View.java:17355)
05-08 12:51:35.762: E/WindowManager(762):   at android.os.Handler.handleCallback(Handler.java:725)
05-08 12:51:35.762: E/WindowManager(762):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 12:51:35.762: E/WindowManager(762):   at android.os.Looper.loop(Looper.java:137)
05-08 12:51:35.762: E/WindowManager(762):   at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 12:51:35.762: E/WindowManager(762):   at java.lang.reflect.Method.invokeNative(Native Method)
05-08 12:51:35.762: E/WindowManager(762):   at java.lang.reflect.Method.invoke(Method.java:511)
05-08 12:51:35.762: E/WindowManager(762):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 12:51:35.762: E/WindowManager(762):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 12:51:35.762: E/WindowManager(762):   at dalvik.system.NativeStart.main(Native Method)
05-08 12:51:35.812: I/Choreographer(762): Skipped 172 frames!  The application may be doing too much work on its main thread.
05-08 12:51:36.172: I/Process(762): Sending signal. PID: 762 SIG: 9

Whilst it doesn't appear to be obviously related, in the logcat you post, the code is executing: 尽管它似乎没有明显的相关性,但是在您发布的logcat中,代码正在执行:

JSONObject json = jsonParser.makeHttpRequest(url_create_product,
            "POST", params);

when the code is terminated. 代码终止时。 It also appears that this request is failing ("unable to connect to localhost"). 还似乎此请求失败(“无法连接到本地主机”)。 Could it be that when this request fails something else is getting onto the the UI thread somehow. 可能是当此请求失败时,其他某种方式会以某种方式进入UI线程。 Either way, this exception should probably be trapped and handled more cleanly. 无论哪种方式,都应该将异常捕获并更干净地处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该应用程序可能在其主线程上做了大量工作(Android) - The application may be doing too much work on its main thread, (Android) Android-该应用程序可能在其主线程上做了大量工作 - Android - The application may be doing too much work on its main thread MapFragment-应用程序可能在其主线程上做过多的工作 - MapFragment - The application may be doing too much work on its main thread 跳过32帧! 该应用程序可能会在其主线程上执行过多的工作,然后执行FATAL EXCEPTION:main - Skipped 32 frames! The application may be doing too much work on its main thread and then FATAL EXCEPTION: main 单击按钮创建BaiDuMap时,请解决此错误:应用程序可能在其主线程上执行了太多工作 - When click a button to create a BaiDuMap,thread this error:The application may be doing too much work on its main thread 跳过104帧! 该应用程序可能在其主线程上做过多的工作 - Skipped 104 frames! The application may be doing too much work on its main thread ImageButton问题。 跳了35帧! 该应用程序可能在其主线程上做过多的工作 - ImageButton issues. Skipped 35 frames! The application may be doing too much work on its main thread 如何避免应用程序可能在其主线程上做过多的工作 - How to avoid The application may be doing too much work on its main thread 跳过49帧!应用程序可能在其主线程上做了太多工作 - Skipped 49 frames! The application may be doing too much work on its main thread android 跳过44帧! 应用程序可能在其主线程上做了太多工作 - android Skipped 44 frames! The application may be doing too much work on its main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM