2

I know a there are lots of people have asked this same thing but I just don't know what's going on. I'm trying to make function like this which consists of 2 EditTexts, 2 ListViews and 1 button. For my apps, user will put input inside EditText (1 for user location and another 1 for destination) the purpose I include ListView is when the user enter input 1 or 2 letters, the listView show suggestion of the places regarding the letters. However, the ListView is hidden - it only will show the list whenever the user have put input inside the EditText.

My code didn't show any errors, but when I run logCat gives me this:

07-02 15:57:39.160: E/AndroidRuntime(5545): FATAL EXCEPTION: main
07-02 15:57:39.160: E/AndroidRuntime(5545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.series3/com.example.series3.FindMePlace}: java.lang.NullPointerException
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.os.Looper.loop(Looper.java:123)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at java.lang.reflect.Method.invoke(Method.java:507)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at dalvik.system.NativeStart.main(Native Method)
07-02 15:57:39.160: E/AndroidRuntime(5545): Caused by: java.lang.NullPointerException
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.example.series3.FindMePlace.onCreate(FindMePlace.java:80)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-02 15:57:39.160: E/AndroidRuntime(5545):     ... 11 more

seriously i really have no ideas where i'm wrong. please help me solve this problem here is my code:

FindMePlace.java

public class FindMePlace extends Activity {

    // ------------------------------------------------------------------
    // Declaration
    public static UkmRoute selectedPath = null;
    final DatabaseHandler db = new DatabaseHandler(this);
    private EditText filterText = null;
    private EditText filterText2 = null;
    ArrayAdapter<String> adapter = null;
    final ArrayList<String> results = new ArrayList<String>();
    final ArrayList<String> results_id = new ArrayList<String>();
    Button search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findmeplace);
        // final Intent c = new Intent(FindMePlace.this,
        // QSLocationDetail.class);

        // ------------------------------------------------------------------
        // Link editText to layout item
        filterText = (EditText) findViewById(R.id.search_Location);
        filterText.addTextChangedListener(filterTextWatcher);
        filterText2 = (EditText) findViewById(R.id.search_Destination);
        filterText2.addTextChangedListener(filterTextWatcher);

        /*populateFromLocation();
        populateToDestination();*/

            // Reading location
            Log.d("Reading", "Reading all location ..");
            List<UkmRoute> location = db.getAllUKMRoute();

            for (UkmRoute k : location) {
                results.add(k.getFromLocation());
                results_id.add(k.getID());
            }
            // Set list arrayAdapter to adapter
            if (!filterText.getText().toString().equals(""))
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, results);
            else {
                ArrayList<String> r = new ArrayList<String>();
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, r);
            }

            // adapter = new ArrayAdapter<String>(this, R.layout.list_item,
            // R.id.textView1, results);
            setListAdapter(adapter);

            // Set ListView from ListActivity
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    Toast.makeText(getApplicationContext(),
                            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

                    Log.d("test", "position:" + position);

                    Log.d("test",
                            "actualname:"
                                    + db.getUkmRouteByFrom(
                                            adapter.getItem(position))
                                            .getFromLocation());

                    // String poiID = results_id.get(position);
                    String poiID = db.getUkmRouteByFrom(adapter.getItem(position))
                            .getID();
                    setSelectedPoi(poiID);
                    // startActivity(c);

                }
            });
//-------------------------------------------------------------------------------

                List<UkmRoute> destination = db.getAllUKMRoute();
                for (UkmRoute k : destination) {
                    results.add(k.getToDestination());
                    results_id.add(k.getID());
                }
                if (!filterText2.getText().toString().equals(""))
                    adapter = new ArrayAdapter<String>(FindMePlace.this,
                            R.layout.list_item, R.id.textView1, results);
                else {
                    ArrayList<String> r = new ArrayList<String>();
                    adapter = new ArrayAdapter<String>(FindMePlace.this,
                            R.layout.list_item, R.id.textView1, r);
                }
                setListAdapter(adapter);

                // Set ListView from ListActivity
                ListView lv2 = getListView();
                lv2.setTextFilterEnabled(true);

                // Set click event from listView
                lv2.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // When clicked, show a toast with the TextView text
                        Toast.makeText(getApplicationContext(),
                                ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

                        Log.d("test", "position:" + position);

                        Log.d("test",
                                "actualname:"
                                        + db.getUkmRouteByTo(adapter.getItem(position))
                                                .getFromLocation());

                        // String poiID = results_id.get(position);
                        String poiID = db.getUkmRouteByTo(adapter.getItem(position))
                                .getID();
                        setSelectedPoi(poiID);
                        // startActivity(c);

                    }
                });

    }

    // Initiate database data
        public void initiateDb() {
            DatabaseHandler myDbHandler = new DatabaseHandler(this);

            try {
                myDbHandler.createDataBase();
            } catch (IOException ioe) {
                throw new Error("Unable to create database");
            }

            try {
                myDbHandler.openDataBase();
            } catch (SQLException sqle) {
                throw sqle;
            }
            Log.d("Initiate",
                    "UKM Route Count: " + myDbHandler.getUkmRouteCount());
            myDbHandler.close();
        }

    // ------------------------------------------------------------------
    private ListView getListView() {
        // TODO Auto-generated method stub
        return null;
    }

    private void setListAdapter(ArrayAdapter<String> adapter2) {
        // TODO Auto-generated method stub

    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (!filterText.getText().toString().equals(""))
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, results);
            else {
                ArrayList<String> r = new ArrayList<String>();
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, r);
            }
            setListAdapter(adapter);
            adapter.getFilter().filter(s);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
    }

    public UkmRoute getSelectedPoi() {
        return selectedPath;
    }

    public void setSelectedPoi(String ID_route) {
        selectedPath = db.getUkmRoute(ID_route);
        Log.d("test2", "ID_route:" + db.getUkmRoute(ID_route).getID());
        Log.d("test2", "FromLocation:" + db.getUkmRoute(ID_route).getFromLocation());
        // kene buat if else ke kalau nk tmbah part Destination?
        // Closing db
        db.close();
    }


}

findmeplace.xml

<RelativeLayout 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: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=".FindMePlace" >

    <EditText
        android:id="@+id/search_Destination"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_box"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="@string/edittext_hint1"
        android:inputType="text"
        android:maxLines="1" />

    <EditText
        android:id="@+id/search_Location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="138dp"
        android:ems="10"
        android:hint="@string/edittext_hint"
        android:inputType="text"
        android:maxLines="1" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/search_box"
        android:layout_marginBottom="71dp"
        android:text="@string/searchbutton" />

    <ListView
        android:id="@+id/inputLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_Location"
        android:layout_below="@+id/search_Location"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="20dp" >
    </ListView>

    <ListView
        android:id="@+id/inputDestination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_Destination"
        android:layout_below="@+id/search_Destination"
        android:layout_marginTop="15dp" >
    </ListView>

</RelativeLayout>

list_item

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
4
  • 1
    what's line 80 in FindMePlace.java? Commented Jul 2, 2013 at 16:29
  • this is line 80 lv.setTextFilterEnabled(true); Commented Jul 2, 2013 at 16:31
  • extend ListActivity. Also show us xml code. Commented Jul 2, 2013 at 16:33
  • alright. i already update the question by adding the xml Commented Jul 2, 2013 at 16:37

3 Answers 3

1

Your listview is not initialized.

      ListView lv = getListView(); // lv is null

http://developer.android.com/reference/android/app/ListActivity.html

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

If you extend ListActivity you can have single listview.

     <ListView
    android:id="@android:id/list" 

Your class does not extend ListActivtiy. Make sure you extend ListActivity

If you extend Activity you should do the below

    ListView lv = (ListView) findViewById(R.id.inputLocation); 
    lv.setAdapter(adapter);

Also you have methods as below which does nothing and should be removed.

private ListView getListView() {
    // TODO Auto-generated method stub
    return null;
}

private void setListAdapter(ArrayAdapter<String> adapter2) {
    // TODO Auto-generated method stub

}
Sign up to request clarification or add additional context in comments.

14 Comments

yah. i know.i read from lots of website, when we did multiple listview, i should use Activity instead of ListActivity.
multiple listview? i din't understand what you are trying to tell. setListAdapter is method of ListActivity
i mean i have two listview for 1 activity
@riehime23 when using getListView() you can't have two lists.. How can the computer know what list you mean?
you are getting confused i guess. Activity does not have setListAdapter and you can't initialize Listview as lv= getListView() if you extend activity. if you extend activity initialize like ListView lv = (ListView) findViewById(R.id.inputLocation) and set adapter to lsitview as lv.setAdapter(adapter).
|
0

Your getListView() method returns hardcoded null. Leading to the NPE one line below for setTextFilterEnabled

Comments

0

You haven't extended ListActivity, thus you can't use getListView()..

You should find it by id when you don't extend ListActivity. if your list has the id equal to list then:

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

By the way it can have any id..

I think ListActivity kind of makes you limited to only the id list.. I don't like that behavior.

However ListActivity has some good methods to work with lists, but I like Activity better...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.