-3

私のアクティビティは Android 4.1 では動作しませんが、2.2、2.3、および 4.0 では正常に動作します。以下に貼り付けたlogcatにいくつかのエラーが表示されます。

何が起こっている?

public class fifthscreen extends Activity {
    String num = null;
    TextView ingredient;
    ImageButton slideHandleButton;

    String name;
    String name2;
    long Menu_ID;
    String dish_name;
    String Type;
    String status;
    String message;
    // HorizontalListView listview;
    // CategoryListAdapter3 cla;
    String DescriptionAPI;
    // ImageLoader2 imgLoader;
    TextView txt1, txt2, txt3;
    ImageView img1;
    String URL, URL2;
    String SelectMenuAPI;
    static ArrayList < Long > Category_ID = new ArrayList < Long > ();
    static ArrayList < String > Category_name = new ArrayList < String > ();
    static ArrayList < String > Category_image = new ArrayList < String > ();
    public static String allergen2;

    // private AQuery androidAQuery;

    @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fifthscreen);
    ingredient = (TextView) findViewById(R.id.ingredient);
    img1 = (ImageView) findViewById(R.id.test_button_image);

    txt1 = (TextView) findViewById(R.id.menuname);
    // txt2 = (TextView) findViewById(R.id.test_button_text1);
    txt3 = (TextView) findViewById(R.id.description);

    Intent iGet = getIntent();

    ImageView options = (ImageView) findViewById(R.id.options5);
    // androidAQuery = new AQuery(this);

     options.setOnClickListener(new OnClickListener() {
                    @Override public void onClick(View v) {
                    Intent iMenuList = new Intent(fifthscreen.this, LinkButtons.class); startActivity(iMenuList);}});

    dish_name = iGet.getStringExtra("dish_name");

    ImageView btnback = (ImageView) findViewById(R.id.btnback);

    btnback.setOnClickListener(new OnClickListener() {
                   @Override public void onClick(View v) {
                   finish();}
                   });

    parseJSONData();

    }

    void clearData() {
    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();

    }

    public void parseJSONData() {

    SelectMenuAPI = Utils.dishdescription + dish_name;

    // SelectMenuAPI = Utils.dishdescription;

    clearData();
    URL = SelectMenuAPI;
    URL2 = URL.replace(" ", "%20");

    // URL2 = SelectMenuAPI;

    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(URL2);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
        str += line;
        }

        JSONObject json2 = new JSONObject(str);

        status = json2.getString("status");
        if (status.equals("1")) {

        JSONArray school2 = json2.getJSONArray("data");
        String[]mVal = new String[school2.length()];

        for (int i = 0; i < school2.length(); i++) {

            String name = school2.getJSONObject(0).getString("name");
            txt1.setText(name);

            String description = school2.getJSONObject(0).getString("description");

            txt3.setText(description);

            String url1 = school2.getJSONObject(0).getString("image");

            androidAQuery.id(img1).image(url1, false, false);

        }

        JSONObject school3 = json2.getJSONObject("dish_nutrition");

        final TableLayout table = (TableLayout) findViewById(R.id.table2);

        for (int j = 0; j < school3.length(); j++) {

            String s = String.valueOf(j + 1);

            final View row = createRow(school3.getJSONObject(s));
            table.addView(row);

        }

        JSONArray school4 = json2.getJSONArray("dish_allergen");
        //
        for (int i = 0; i < school4.length(); i++) {
            JSONObject object = school4.getJSONObject(i);

            Category_ID.add((long) i);
            Category_name.add(object.getString("name"));
            Category_image.add(object.getString("image"));
            listview.setAdapter(cla);

        }

        final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);

        JSONArray school5 = json2.getJSONArray("dish_ingredient");

        for (int i = 0; i < school5.length(); i++) {
            JSONObject jsonObject = school5.getJSONObject(i);

            final View row2 = createRow2(school5.getJSONObject(i));
            table3.addView(row2);

        }

        }

        else {

        JSONArray school2 = json2.getJSONArray("data");
        for (int i = 0; i < school2.length(); i++) {
            JSONObject object = school2.getJSONObject(i);

            Category_ID.add((long) i);
            Category_name.add(object.getString("name"));

        }

        }

    } catch(MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch(IOException e) {
        // TODO Auto-generated catch block
        // IOConnect = 1;
        e.printStackTrace();
    } catch(JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

    public View createRow(JSONObject item) throws JSONException {
    View row = getLayoutInflater().inflate(R.layout.rows, null);
     ((TextView) row.findViewById(R.id.localTime)).setText(item.getString("qty"));
     ((TextView) row.findViewById(R.id.apprentTemp)).setText(item.getString("name"));

     return row;
    } public View createRow2(JSONObject item) throws JSONException {

    View row2 = getLayoutInflater().inflate(R.layout.row2, null);
     ((TextView) row2.findViewById(R.id.name)).setText(item.getString("name"));
     ((TextView) row2.findViewById(R.id.subingredients)).setText(item.getString("sub_ingredients"));

     return row2;
}}

ログキャット:

 Skipped 74 frames!  The application may be doing too much work on its main thread.
 Skipped 61 frames!  The application may be doing too much work on its main thread.
  Shutting down VM
  threadid=1: thread exiting with uncaught exception (group=0x40a71930)
 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity   
 ComponentInfo{com.schoollunchapp/com.schoollunchapp.fifthscreen}: 
  android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
      (DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at  
    org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at  
    org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.schoollunchapp.fifthscreen.parseJSONData(fifthscreen.java:196)
at com.schoollunchapp.fifthscreen.onCreate(fifthscreen.java:157)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
4

2 に答える 2

0

すべてのネットワーク関連の操作は、UI スレッドでは実行されません。これが問題の原因である必要があります。詳細については。 http://developer.android.com/training/basics/network-ops/connecting.html

于 2013-10-01T12:21:42.070 に答える
0

がないために起こりAsyncTaskます。すべてのHttpClientロジックを に配置しAsyncTaskます。

ログから:

android.os.NetworkOnMainThreadException

parseJSONDataメインスレッドでは実行できません。

これを実装する方法の例を次に示します。

 private class AsyncRequestConnection extends AsyncTask<Void, Void, String> {
    private final Request request;

    public AsyncRequestConnection(Request request) {
        this.request = request;
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            HttpPost p = createRequestHttpMessage(request);
            String resp = new String(stripResponse(getRequestClient().execute(p)));
            Log.v(TAG, resp);
            return resp;
        } catch (Exception e) {
            Log.e(TAG, "Cannot complete API request", e);
            cancel(false);
            return null;
        }
    }

    @Override
    protected void onCancelled() {
        request.backoff();
        if (request.hasReachedMaxBackoff()) {
            request.cancel(R.string.error_internal);
        } else {
            requestQueue.enqueue(request);              
        }
        requestConnection = null;
        nextRequest();
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            request.attachResponse(result);
            request.handleResponse();               
            requestConnection = null;
            nextRequest();
        } else {
            request.cancel(R.string.error_internal);
        }
    }
}

あなたのアクティビティからそれを呼び出すだけです:

 AsyncRequestConnection task = new AsyncRequestConnection ();
 task.execute();    
于 2013-10-01T12:17:05.837 に答える