-1

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



}
4

2 に答える 2

1

Fragment の で HTTP リクエストを作成していますonCreate()。これは非常に悪い考えであり、システムが文句を言うのは当然のことです。ネットワーク リクエストが完了するまでにかかる時間はわかりません。その間、UI スレッドはブロックされて結果を待ちます。

ドキュメントから:

UI スレッドをブロックしない

ネットワーク アクティビティはバックグラウンド スレッドで発生する必要があります。ASyncTaskまたはをご覧くださいIntentService

于 2013-02-13T11:09:09.353 に答える