0

我是android编程的新手。我想创建一个搜索活动,我确实无法做到。以下是 java 代码、xml 代码和我的 logCat。Logcat 总是要求“强制关闭”。我正在 android 2.2 上构建这个应用程序。请帮忙。

这是 searchActivity.java 代码:

private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> schemeList;
private ListView lv;
// products JSONArray
JSONArray scheme_array = null;

EditText inputSearch;
ArrayAdapter<HashMap<String, String>> adapter;

// Inbox JSON url
private static final String INBOX_URL = "http://115.240.97.85/Schemes_NAV/schemes.json";

// ALL JSON node names
private static final String TAG_SCHEME = "scheme";
private static final String TAG_ID = "id";
private static final String TAG_FROM = "id_from";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_LASTPRICE = "last_price";
private static final String TAG_CHANGE = "change";


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

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

    // Loading INBOX in Background Thread
    new LoadInbox().execute();

    ListView lv = (ListView)findViewById(R.id.mfList); // create a list view


    inputSearch = (EditText) findViewById(R.id.EditText01);
    adapter = new ArrayAdapter<HashMap<String, String>>(this, R.layout.mf_search, schemeList);
    lv.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            searchActivity.this.adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
}

/**
 * Background Async Task to Load all INBOX messages by making HTTP Request
 * */
class LoadInbox extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(searchActivity.this);
        pDialog.setMessage("Loading Schemes ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Inbox JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                params);

        // Check your log cat for JSON response
        Log.d("Scheme JSON: ", json.toString());

        try {
            scheme_array= json.getJSONArray(TAG_SCHEME);
            // looping through All messages
            for (int i = 0; i < scheme_array.length(); i++) {
                JSONObject c = scheme_array.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String id_from = c.getString(TAG_FROM);
                String subject = c.getString(TAG_SUBJECT);
                String lastprice = c.getString(TAG_LASTPRICE);

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

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_FROM, id_from);
                map.put(TAG_SUBJECT, subject);
                map.put(TAG_LASTPRICE, lastprice);

                // adding HashList to ArrayList
                schemeList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        searchActivity.this, schemeList,
                        R.layout.inbox_list_item, new  String[] { TAG_FROM},
                        new int[] { R.id.from });
                // updating listview
                setListAdapter(adapter);
            }

        });
    }


}}

以下显示 xml 即 inbox_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >


<TextView
    android:id="@+id/from"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:paddingBottom="4dip"
    android:paddingLeft="8dip"
    android:paddingTop="8dip"
    android:textColor="@android:color/white"
    android:textSize="20dip"
     />

<CheckBox
    android:id="@+id/CheckBox01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="16dp"
    android:clickable="true" />

这是我的 mf_search.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/a"
android:orientation="vertical" >

<EditText
    android:id="@+id/EditText01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/Search" >
</EditText>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="378dp"
    android:layout_weight="925.26"
    android:clickable="true" >

</ListView>


<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add" />

这是我的 LogCat

03-10 02:14:16.637: D/dalvikvm(569): GC_EXTERNAL_ALLOC freed 720 objects / 53960 bytes in 153ms
03-10 02:14:24.728: D/dalvikvm(569): GC_EXTERNAL_ALLOC freed 486 objects / 26136 bytes in 93ms
03-10 02:14:25.868: D/AndroidRuntime(569): Shutting down VM
03-10 02:14:25.868: W/dalvikvm(569): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-10 02:14:25.917: E/AndroidRuntime(569): FATAL EXCEPTION: main
03-10 02:14:25.917: E/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nav2/com.example.nav2.searchActivity}: java.lang.NullPointerException
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.os.Looper.loop(Looper.java:123)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-10 02:14:25.917: E/AndroidRuntime(569):  at java.lang.reflect.Method.invokeNative(Native Method)
03-10 02:14:25.917: E/AndroidRuntime(569):  at java.lang.reflect.Method.invoke(Method.java:521)
03-10 02:14:25.917: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-10 02:14:25.917: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-10 02:14:25.917: E/AndroidRuntime(569):  at dalvik.system.NativeStart.main(Native Method)
03-10 02:14:25.917: E/AndroidRuntime(569): Caused by: java.lang.NullPointerException
03-10 02:14:25.917: E/AndroidRuntime(569):  at com.example.nav2.searchActivity.onCreate(searchActivity.java:82)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-10 02:14:25.917: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-10 02:14:25.917: E/AndroidRuntime(569):  ... 11 more
03-10 02:14:30.177: I/Process(569): Sending signal. PID: 569 SIG: 9    
4

2 回答 2

1

利用

   ListView lv = (ListView) findViewById(android.R.id.list);

或者

  ListView lv=this.getListView();

代替

ListView lv = (ListView)findViewById(R.id.mfList); 

因为您在 xml 中有 ListView idandroid:id="@android:id/list"但试图找到R.id.mfList

于 2013-03-09T21:15:05.123 回答
0

使用ListView lv = (ListView) findViewById(android.R.id.list),您可以在 XML 中看到您的列表 ID 是android:id="@android:id/list但您的目标ListView lv = (ListView)findViewById(R.id.mfList)不存在

于 2013-03-09T21:20:03.590 回答