致命异常:由于没有互联网连接,AsyncTask#1?

时间:2013-11-28 10:51:40

标签: java android

我目前手机上没有3g,我正试图通过WIFI做这一切,有什么建议吗?我想这就是我收到这个错误的原因。 这是Async类;

    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;
    }

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

错误日志

          11-28 05:30:40.525: E/WindowManager(6132): Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@425a2140 that was originally added here
                  11-28 05:30:40.525: E/WindowManager(6132): android.view.WindowLeaked: Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@425a2140 that was originally added here
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:409)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.Window$LocalWindowManager.addView(Window.java:559)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.app.Dialog.show(Dialog.java:277)
                  11-28 05:30:40.525: E/WindowManager(6132):    at com.example.androidhive.NewProductActivity$CreateNewProduct.onPreExecute(NewProductActivity.java:76)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.os.AsyncTask.execute(AsyncTask.java:534)
                  11-28 05:30:40.525: E/WindowManager(6132):    at com.example.androidhive.NewProductActivity$1.onClick(NewProductActivity.java:56)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.View.performClick(View.java:4192)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.view.View$PerformClick.run(View.java:17254)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.os.Handler.handleCallback(Handler.java:615)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.os.Handler.dispatchMessage(Handler.java:92)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.os.Looper.loop(Looper.java:137)
                  11-28 05:30:40.525: E/WindowManager(6132):    at android.app.ActivityThread.main(ActivityThread.java:4950)
                  11-28 05:30:40.525: E/WindowManager(6132):    at java.lang.reflect.Method.invokeNative(Native Method)
                  11-28 05:30:40.525: E/WindowManager(6132):    at java.lang.reflect.Method.invoke(Method.java:511)
                  11-28 05:30:40.525: E/WindowManager(6132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
                  11-28 05:30:40.525: E/WindowManager(6132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
                  11-28 05:30:40.525: E/WindowManager(6132):    at dalvik.system.NativeStart.main(Native Method)

我的AndroidManifest.xml

      <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidhive"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application

    android:configChanges="keyboardHidden|orientation"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >


    <activity
        android:name=".MainScreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- All Product Activity -->
    <activity
        android:name=".AllProductsActivity"
        android:label="All Products" >
    </activity>

    <!-- Add Product Activity -->
    <activity
        android:name=".NewProductActivity"
        android:label="Add New Product" >
    </activity>

    <!-- Edit Product Activity -->
    <activity
        android:name=".EditProductActivity"
        android:label="Edit Product" >
    </activity>

</application>


<!--  Internet Permissions -->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

3 个答案:

答案 0 :(得分:0)

<uses-permission android:name="android.permission.INTERNET" />

同时添加此权限

答案 1 :(得分:0)

我认为你不应该在doInBackground开始一项新活动。 doInBackground应该从网站获取信息并将其传递到onPostExecute,您应该移动代码以打开新活动。

答案 2 :(得分:0)

您在退出活动后尝试显示对话框。 见Activity has leaked window that was originally added