1

我对 Android 编程很陌生,所以任何帮助都会很棒:) 我正在尝试远程获取一些虚拟数据,解析 json 响应并将其显示在列表视图中。

这是我尝试与列表活动一起使用的 xml 布局文件:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    />
</LinearLayout>

这是我的 http 搜索活动:-

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class searchResultActivity extends ListActivity {

    ListView listview;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    String url;
    String fd_name;
    ArrayList<String> listItems;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        /*setContentView(R.layout.test);
        listview = (ListView)findViewById(R.id.list);*/
        url = "http://pjchambers.comuf.com/index.php";
        new getData().execute();
    }

    public class getData extends AsyncTask<String, Void, ArrayList<String>> {

        protected ArrayList<String> doInBackground(String...urls) {
            // TODO Auto-generated method stub
            listItems = new ArrayList<String>();

            try {
                // Set of http statements setting up the connections to the php
                // and database
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }

            // convert response to string
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            // paring data
            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    fd_name = json_data.getString("Name");
                    listItems.add(fd_name);
                }
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No Results Found",
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            Log.d("debug", "- " + listItems.toString());
            return listItems;
        }

        protected void doPostExecute() {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    searchResultActivity.this, android.R.layout.simple_list_item_1, R.id.list, listItems);
            setListAdapter(adapter);
        }

    }

}

我花了几个小时尝试不同的方法和方法,使用了一些不同的在线教程,但它们都以相同的结果结束。就目前而言,该应用程序可以正常工作,但会显示显示;但是,如果我在 onCreate 方法中声明布局,应用程序就会崩溃。必须感谢任何帮助或建议,新的眼睛可能会立即注意到问题

这是我取消注释这两个语句后的 logcat

05-22 12:12:20.234: D/debug(631): - [The Arena, Empire]
05-22 12:12:42.004: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:42.244: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
05-22 12:12:42.404: D/gralloc_goldfish(691): Emulator without GPU emulation detected.
05-22 12:12:48.415: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:48.424: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
05-22 12:12:50.595: D/AndroidRuntime(691): Shutting down VM
05-22 12:12:50.595: W/dalvikvm(691): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-22 12:12:50.614: E/AndroidRuntime(691): FATAL EXCEPTION: main
05-22 12:12:50.614: E/AndroidRuntime(691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.barcrawl/com.project.barcrawl.searchResultActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.os.Looper.loop(Looper.java:137)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-22 12:12:50.614: E/AndroidRuntime(691):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 12:12:50.614: E/AndroidRuntime(691):  at java.lang.reflect.Method.invoke(Method.java:511)
05-22 12:12:50.614: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 12:12:50.614: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 12:12:50.614: E/AndroidRuntime(691):  at dalvik.system.NativeStart.main(Native Method)
05-22 12:12:50.614: E/AndroidRuntime(691): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-22 12:12:50.614: E/AndroidRuntime(691):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.Activity.setContentView(Activity.java:1835)
05-22 12:12:50.614: E/AndroidRuntime(691):  at com.project.barcrawl.searchResultActivity.onCreate(searchResultActivity.java:41)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.Activity.performCreate(Activity.java:4465)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-22 12:12:50.614: E/AndroidRuntime(691):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-22 12:12:50.614: E/AndroidRuntime(691):  ... 11 more
05-22 12:12:51.004: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:51.024: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
4

1 回答 1

2

您在 XML 中的 id 应该id="@android:id/list"可以工作。在您的代码中,将创建一个新 id 作为“您的” R.class 的一部分,它不同于android.R.id.list. 因此无法找到列表视图并因此引发异常。

此外,构造函数的第三个参数ArrayAdapter应该是要填充数据的文本资源的 id。你正在通过R.id.list这是错误的。如果我没记错的话,android.R.id.simple_list_item_1它应该是它定义的资源。android.R.id.text1

于 2012-05-22T12:54:35.610 回答