android logcat中的运行时异常

时间:2013-04-25 19:02:07

标签: android android-logcat runtimeexception

我想从服务器(WAMP)数据库中检索数据,我希望在单个列表视图中显示。我花了很长时间才了解这个,最后我实现了但是logcat显示了几个android运行时错误。我修正了一些错误,并发布了一些我无法解决的错误。 我的.java文件:

 package com.example.least;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    // url to make request
    private static String url = "http://localhost/android_connect/final.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "opass";
    private static final String TAG_ID = "User_ID";

    // contacts JSONArray
    JSONArray contacts = null;

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

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

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

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);

                // Phone number is agin JSON Object

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

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.list_item,
                new String[] { TAG_ID }, new int[] {
                        R.id.textView1});

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String name = ((TextView) view.findViewById(R.id.textView1)).getText().toString();


                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(TAG_ID, name);

                startActivity(in);
            }
        });
    }

}

logcat例外:

04-25 19:35:02.353: D/AndroidRuntime(5650): Shutting down VM
04-25 19:35:02.353: W/dalvikvm(5650): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-25 19:35:02.393: E/AndroidRuntime(5650): FATAL EXCEPTION: main
04-25 19:35:02.393: E/AndroidRuntime(5650): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.least/com.example.least.MainActivity}: android.os.NetworkOnMainThreadException
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.os.Looper.loop(Looper.java:137)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at java.lang.reflect.Method.invoke(Method.java:511)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at dalvik.system.NativeStart.main(Native Method)
04-25 19:35:02.393: E/AndroidRuntime(5650): Caused by: android.os.NetworkOnMainThreadException
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at com.example.least.JSONParser.getJSONFromUrl(JSONParser.java:39)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at com.example.least.MainActivity.onCreate(MainActivity.java:45)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.Activity.performCreate(Activity.java:5104)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-25 19:35:02.393: E/AndroidRuntime(5650):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-25 19:35:02.393: E/AndroidRuntime(5650):     ... 11 more
04-25 19:35:02.603: D/dalvikvm(5650): GC_CONCURRENT freed 211K, 13% free 2588K/2944K, paused 24ms+30ms, total 226ms

主Activity.xml文件:

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list">
</ListView>

因此我是android新手我正在努力解决这些类型的错误。请帮我修复

2 个答案:

答案 0 :(得分:1)

问题是Caused by: java.lang.RuntimeException: Binary XML file line #1: You must supply a layout_width attribute.,它是在at com.example.least.MainActivity.onCreate(MainActivity.java:36)

引起的

请检查布局文件R.layout.activity_main,其中缺少layout_width属性。

顺便说一下,尽管你是Android的新手,你至少应该在将它发布到网上之前阅读logcat,因为在很多情况下(这是其中之一),logcat会告诉你错误在哪里以及它是由什么引起的

答案 1 :(得分:-1)

问题是您正在尝试在create上执行与网络相关的操作。 Android只允许与网络相关的操作,例如从线程内的Web服务或AsyncTask获取值。所以尝试添加Asynctask或线程