是什么导致我的Android应用程序中出现这些错

时间:2013-02-13 11:06:22

标签: android xmlhttprequest

我在LogCat上收到这些错误:

02-13 18:52:12.742: E/AndroidRuntime(428): FATAL EXCEPTION: main
02-13 18:52:12.742: E/AndroidRuntime(428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.os.Looper.loop(Looper.java:132)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread.main(ActivityThread.java:4025)
02-13 18:52:12.742: E/AndroidRuntime(428):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 18:52:12.742: E/AndroidRuntime(428):  at java.lang.reflect.Method.invoke(Method.java:491)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-13 18:52:12.742: E/AndroidRuntime(428):  at dalvik.system.NativeStart.main(Native Method)
02-13 18:52:12.742: E/AndroidRuntime(428): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.Activity.setContentView(Activity.java:1780)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.thesis.menubook.MenuMain.onCreate(MenuMain.java:16)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
02-13 18:52:12.742: E/AndroidRuntime(428):  ... 11 more
02-13 18:52:12.742: E/AndroidRuntime(428): Caused by: android.os.NetworkOnMainThreadException
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
02-13 18:52:12.742: E/AndroidRuntime(428):  at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
02-13 18:52:12.742: E/AndroidRuntime(428):  at java.net.Socket.connect(Socket.java:901)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-13 18:52:12.742: E/AndroidRuntime(428):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.thesis.menubook.JSONParser.makeHttpRequest(JSONParser.java:62)
02-13 18:52:12.742: E/AndroidRuntime(428):  at com.thesis.menubook.MenuCategory.onCreate(MenuCategory.java:41)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:702)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:905)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:983)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.app.Activity.onCreateView(Activity.java:4141)
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
02-13 18:52:12.742: E/AndroidRuntime(428):  ... 20 more
02-13 18:52:13.241: D/dalvikvm(428): GC_CONCURRENT freed 335K, 6% free 7443K/7879K, paused 10ms+10ms
02-13 18:52:23.632: I/Process(428): Sending signal. PID: 428 SIG: 9

我猜这个特定的行导致我的应用程序强行关闭:

02-13 18:52:12.742: E/AndroidRuntime(428): Caused by: android.os.NetworkOnMainThreadException
02-13 18:52:12.742: E/AndroidRuntime(428):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)

任何想法导致了什么?这是我的java文件:

package com.thesis.menubook;


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

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MenuCategory extends ListFragment {
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> categoryList;
    private ProgressDialog pDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // LOAD CATEGORY ONTO LIST

        Bundle b = getActivity().getIntent().getExtras();
        String table_ID = b.getString("Table");
        String ipaddress = b.getString("IPAddress");

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        Log.d("IP ADDRESS", ipaddress +" ");

            //CODE FAILS HERE 

        JSONObject json = jsonParser.makeHttpRequest("http://"+ipaddress+"/MenuBook/selectCategories.php", "GET", params);

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

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

            if (success == 1) {
                // products found
                // Getting Array of Products

                JSONArray category_list = json.getJSONArray("category_list");

                // looping through All Products
                for (int j = 0; j < category_list.length(); j++) {
                    JSONObject c = category_list.getJSONObject(j);

                    // Storing each json item in variable
                    String category = c.getString("category");


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

                    // adding each child node to HashMap key => value
                    map.put("category", category);
                    int num = 1;
                    Log.d("category #"+num+"", category);
                    num++;
                    // adding HashList to ArrayList
                    if(categoryList.contains(map) != true)
                    {
                        categoryList.add(map);
                    }
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ArrayAdapter<ArrayList<HashMap<String, String>>> arrayAdapter = new ArrayAdapter<ArrayList<HashMap<String, String>>>(getActivity().
                getApplicationContext(), R.layout.activity_menu_category);
        arrayAdapter.add(categoryList);
        setListAdapter(arrayAdapter);
    }



}

2 个答案:

答案 0 :(得分:3)

使用ASyncTask执行网络任务。从Honeycomb开始,禁止在UI线程上执行网络请求,并使用此异常进行处理。

答案 1 :(得分:1)

您正在片段onCreate()中发出HTTP请求。这是一个非常糟糕的主意,并且正确地导致系统抱怨。您不知道网络请求需要多长时间才能完成,同时UI线程被阻塞等待结果。

来自the documentation

  

不要阻止UI线程

网络活动应该在后台线程中进行。请查看ASyncTaskIntentService