简体   繁体   中英

Unable to start Search Activity in android

I'm new to android Programming. I want to create a search Activity, which indeed I'm not able to make it out. Following is the java code, xml code and my logCat. Logcat always ask for "force close". I'm building this app on android 2.2. Please Help.

Here is searchActivity.java code:

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);
            }

        });
    }


}}

Following shows xml ie 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" />

here is my 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" />

and Here is my 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    

use

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

OR

  ListView lv=this.getListView();

instead of

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

because you have ListView id android:id="@android:id/list" in xml but trying to find R.id.mfList

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM