列表视图中的问题从MYSQL数据库中检索

时间:2016-05-10 13:36:59

标签: java android mysql json

我的问题是,当我运行应用程序时,不幸的是应用程序已经停止发生错误,任何人都可以给出解决方案。主要发生的异常是什么错误。请帮助我找出这个解决方案吗?

 public class JSONPrser extends ListActivity
{


/**
     * Called when the activity is first created.
     */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,this.populate()));
    }

    private ArrayList<String> populate() {
        ArrayList<String> items = new ArrayList<String>();

        try {
            URL url = new URL("http://something.com/webservices/categories.php");
            HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            // gets the server json data
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String next;
            while ((next = bufferedReader.readLine()) != null){
                JSONArray ja = new JSONArray(next);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    items.add(jo.getString("category name"));
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return items;
    }
}

logcat的

E/AndroidRuntime:




  FATAL EXCEPTION: main


             java.lang.RuntimeException: Unable to start activity ComponentInfo{anilkumar.com.yoursub/anilkumar.com.yoursub.JSONPrser}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                 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:784)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                 at android.app.ListActivity.onContentChanged(ListActivity.java:243)
                                                 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
                                                 at android.app.Activity.setContentView(Activity.java:1835)
                                                 at anilkumar.com.yoursub.JSONPrser.onCreate(JSONPrser.java:31)
                                                 at android.app.Activity.performCreate(Activity.java:4466)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:123) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:4424) 
                                                 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:784) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
                                                 at dalvik.system.NativeStart.main(Native Method) 

Xml代码

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="anilkumar.com.yoursub.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>

1 个答案:

答案 0 :(得分:0)

请检查布局中的布局(xml),您需要将ID更改为 android.R.id.list