强制关闭有很多错误[重复]

Posted

技术标签:

【中文标题】强制关闭有很多错误[重复]【英文标题】:Force closes with lots of error [duplicate] 【发布时间】:2013-04-09 08:04:53 【问题描述】:

谁能告诉我,这里有什么问题?我一直在尝试一整天。我是 android 新手,我也不擅长阅读 logcat,因此,我真的希望有人能帮助我。应用力一直关闭。

private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> eventsList;

    // url to get all products list
    private static String url_all_products = "http://xx.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CUSTOMER = "customer";
    private static final String TAG_PID = "pid";
    private static final String TAG_FIRSTNAME = "firstname";
    private static final String TAG_LASTNAME = "lastname";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_TOTALCOST = "totalCost";

    // products JSONArray
    JSONArray events = null;

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

        // Hashmap for ListView
        eventsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();

    


    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> 

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            pDialog = new ProgressDialog(CustomerTransaction.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try 
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) 
                    // products found
                    // Getting Array of Products
                    events = json.getJSONArray(TAG_CUSTOMER);

                    // looping through All Products
                    for (int i = 0; i < events.length(); i++) 
                        JSONObject c = events.getJSONObject(i);

                        // Storing each json item in variable
                        String pid = c.getString(TAG_PID);
                        String firstname = c.getString(TAG_FIRSTNAME);
                        String lastname = "Time :" +c.getString(TAG_LASTNAME);
                        String address = "Date :" +c.getString(TAG_ADDRESS);
                        String totalCost = "Venue :" +c.getString(TAG_TOTALCOST);


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, pid);
                        map.put(TAG_FIRSTNAME, firstname);
                        map.put(TAG_LASTNAME, lastname);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_TOTALCOST, totalCost);
                        // adding HashList to ArrayList
                        eventsList.add(map);
                    
                 

             catch (JSONException e) 
                e.printStackTrace();
            

            return null;
        

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) 
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() 
                public void run() 
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            CustomerTransaction.this, eventsList,
                            R.layout.list_item4, new String[]  TAG_PID,TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS, TAG_TOTALCOST,
                            new int[]  R.id.pid, R.id.firstname, R.id.lastname, R.id.address, R.id.totalCost );
                    // updating listview
                    setListAdapter(adapter);
                
            );

        

    

日志猫:

04-16 18:34:22.706: E/WindowManager(1019): Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView40d2c1e8 V.E..... R.....ID 0,0-304,96 that was originally added here
 04-16 18:34:22.706: E/WindowManager(1019): android.view.WindowLeaked: Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView40d2c1e8 V.E..... R.....ID 0,0-304,96 that was originally added here
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-16 18:34:22.706: E/WindowManager(1019):  at  android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Dialog.show(Dialog.java:281)
 04-16 18:34:22.706: E/WindowManager(1019):     at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 04-16 18:34:22.706: E/WindowManager(1019):     at android.os.AsyncTask.execute(AsyncTask.java:534)
04-16 18:34:22.706: E/WindowManager(1019):  at com.spyraa.bookstore.CustomerTransaction.onCreate(CustomerTransaction.java:61)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Activity.performCreate(Activity.java:5104)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Looper.loop(Looper.java:137)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 18:34:22.706: E/WindowManager(1019):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 18:34:22.706: E/WindowManager(1019):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 18:34:22.706: E/WindowManager(1019):  at dalvik.system.NativeStart.main(Native Method)

非常感谢。

【问题讨论】:

如果您还有其他问题,请将其作为另一个问题发布。 【参考方案1】:

这样使用请求

HttpPost httpost = new HttpPost(url_all_products);

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("test1","test1" ));
nvps.add(new BasicNameValuePair("test2", "test2" ));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = getResponse(httpost);

【讨论】:

嗯..不太明白。除了 test1 和 test 2,我还应该放什么? 如果你有参数然后添加,否则离开它。【参考方案2】:

我看到了几个问题。第一个我认为是造成你的问题。将您的进度条初始化移出onPreExecute() 到类声明中

 class LoadAllProducts extends AsyncTask<String, String, String> 

  pDialog = new ProgressDialog(CustomerTransaction.this);  // move to here so you can cancel in onPostExecute()
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
        super.onPreExecute();

onPostExecute() 中不需要runOnUiThread,因为此方法在UI 线程上运行。您在onPostExecute() 中使用了一个参数,但似乎没有从doInBackground() 返回任何内容。可能还有更多问题,但我会先尝试这些事情,看看你会得到什么

了解如何使用 logcat 非常重要。在这里,它告诉您一个窗口已泄漏,我认为这是由您初始化 progressbar 的位置引起的。通常你会看到一行写着“由空指针异常引起”或其他异常。当您找到此行时,引用您的项目的第一行

 at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)

例如,会告诉你第 84 行是你的错误发生的地方。这通常是开始寻找的好地方。希望这会有所帮助

【讨论】:

是的,知道了。谢谢。 不,有问题。如果这对您有帮助,请接受 它有所帮助,但显示更多错误。 :(你能帮我解决吗? 没有看到更新的 logcat 和新错误的相关代码...... 我在问题中更新为logcat

以上是关于强制关闭有很多错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章

不明白不能强制类型“关闭”错误

如何解决由于强制展开导致的致命意外零错误[重复]

在运行 Android 模拟器时强制关闭应用程序错误

编译器显示错误消息“从不强制转换的整数中生成指针”(反转数组)。我该如何解决这个问题? [关闭]

应用程序在启动 facebook 活动时强制关闭

如何修复`EF Core中的远程主机`错误强制关闭现有连接