4

I browse on some question here in stack overflow but it didn't solve my problem.

here's my code.

   public class MenuListActivity extends ListActivity {
String MenuNames[] = {"Consultation", "Medicine", "Diseases", "Doctor"};
protected void onCreateView(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);
    final ListView lv = (ListView) findViewById(R.id.listView1);

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            if(position == 1) {
                //code specific to first list item    
                Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
                startActivity(myIntent);
            }


        }

    }
     );

}

Logcat.

    10-12 10:53:37.572: E/AndroidRuntime(10605): FATAL EXCEPTION: main
    10-12 10:53:37.572: E/AndroidRuntime(10605): Process: com.capstone.medicinehealthguide, PID:     10605
    10-12 10:53:37.572: E/AndroidRuntime(10605): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.capstone.medicinehealthguide/com.capstone.medicinehealthguide.MenuListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.os.Handler.dispatchMessage(Handler.java:102)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.os.Looper.loop(Looper.java:135)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.main(ActivityThread.java:5070)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at java.lang.reflect.Method.invoke(Native Method)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at java.lang.reflect.Method.invoke(Method.java:372)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    10-12 10:53:37.572: E/AndroidRuntime(10605): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.capstone.medicinehealthguide.MenuListActivity.onCreate(MenuListActivity.java:24)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.Activity.performCreate(Activity.java:5720)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    ... 10 more

XML file

    <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="com.capstone.medicinehealthguide.MenuListActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</ListView>

I'm trying to make an app that has a listview and when you click on it, another activities will be open

Red_X
  • 51
  • 1
  • 1
  • 7

2 Answers2

1

You are actually using this wrong:

final ListView lv = (ListView) getListView().findViewById(R.id.listView1);

Here:

final ListView lv = (ListView) findViewById(R.id.listView1);

The issue is that in yours, you are getting the list view, and then trying to get your list view from a list view.

Try the option above to just get the list view from your content view.

EmmanuelMess
  • 1,025
  • 2
  • 17
  • 31
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • The two options you posted are the same; is that what you intended? (A [suggested edit](http://stackoverflow.com/questions/5584976/android-device-chooser-my-device-seems-offline)) has removed the redundant one. – Pokechu22 Nov 30 '14 at 00:42
0

Before you invoke findByViewId the view must be inflated. What you have to do is.. move this code from onCreate method to 'onCreateView' method. IN onCreateView method place it below setContentView() line..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);

final ListView lv = (ListView) getListView().findViewById(R.id.listView1);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
            startActivity(myIntent);
        }
}
Panther
  • 8,938
  • 3
  • 23
  • 34
  • I did what you told me It didn't crash anymore but it didn't display the list view – Red_X Oct 12 '14 at 18:48
  • To use `setListAdapter` you should extend the `ListActivity`, but you have extended the `ActionbarActivity`. see the edited code above :-/ – Panther Oct 13 '14 at 03:15
  • it says, "adapter cannot be resolve to a variable." :/ – Red_X Oct 13 '14 at 04:28
  • @Red_X you have to decleare the adapter dont forget the first line `ArrayAdapter adapter = new ArrayAdapter(....` – Panther Oct 13 '14 at 06:05
  • I already declare it but still I can't see the list view :( I'll update my code – Red_X Oct 13 '14 at 13:51
  • Instead of using `this` in `new ArrayAdapter` use `MenuListActivity.this`. If you still have error, post it clearly :-/ – Panther Oct 13 '14 at 16:11
  • Actually, I don't have an error now, my new problem is the list view doesn't appear at all :/ – Red_X Oct 13 '14 at 17:24