Question

The situation is when I select one of the list item. It will show the details of the item on the next activity.

I trying to getting a values from selected ListItem by using onItemClick Listener. I got error [FATAL EXCEPTION] while click on the item.

Please help me.

This is part of ScheduleFragment.java

public class ScheduleFragment extends Fragment {
 /* other activity */

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_schedule, container, false);

myListView = (ListView) rootView.findViewById(R.id.list);

    // on selecting single subject
    myListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String matrix_id = ((ListView) view.findViewById(R.id.list)).getText()
                    .toString();

            // Starting new intent
            Intent in = new Intent(getActivity().getApplicationContext(),
                    SingleSubject.class);

            // sending matrix id to next activity
            in.putExtra(TAG_MATRIX_ID, matrix_id);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);
        }
    });

This is SingleSubject.java

public class SingleSubject extends Activity {

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


    // getting subject details from intent
    Intent i = getIntent();

    // getting matrix_id from intent
    matrix_id = i.getStringExtra(TAG_MATRIX_ID);

    // Getting complete matrix_id details in background thread
    new GetSubjectDetails().execute();


}

This single_subject.xml

<!-- Name Label -->
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Name"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:textSize="17dip"/>

<!-- Input Name -->
<EditText android:id="@+id/inputName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_marginBottom="15dip"
    android:singleLine="true"/>

<!-- Email Label -->
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Email"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:textSize="17dip"/>

<!-- Input Email -->
<EditText android:id="@+id/inputEmail" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_marginBottom="15dip"
    android:singleLine="true"/>

This is fragment_schedule.xml

 <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

This is log error.

03-25 14:40:49.808: W/dalvikvm(10182): threadid=1: thread exiting with uncaught exception (group=0x40d09930)
03-25 14:40:49.854: E/AndroidRuntime(10182): FATAL EXCEPTION: main
03-25 14:40:49.854: E/AndroidRuntime(10182): java.lang.NullPointerException
03-25 14:40:49.854: E/AndroidRuntime(10182):    at com.ultra.esc.ScheduleFragment$1.onItemClick(ScheduleFragment.java:95)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.os.Handler.handleCallback(Handler.java:725)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.os.Looper.loop(Looper.java:137)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at android.app.ActivityThread.main(ActivityThread.java:5039)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at java.lang.reflect.Method.invoke(Method.java:511)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 14:40:49.854: E/AndroidRuntime(10182):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

You do not have a listview in the layout that you inflate for the adapter

 String matrix_id = ((ListView) view.findViewById(R.id.list)).getText()
                .toString();

Hence the above initialization gives you NullpointerExcpetion.

Guess work:

I can guess from the previous question that i answered of yours (ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView). You have TextView's for the layout that you inflate in your adapter with ids R.id.matrix_id and R.id.name. But to confirm you can post your adapter code.

Copying from your previous question

 ListAdapter adapter = new SimpleAdapter(
                       getActivity(), subjectList,
                    R.layout.all_subject, new String[] { TAG_MATRIX_ID,
                            TAG_NAME},
                    new int[] { R.id.matrix_id, R.id.name });

You can use getTag and setTag to avoid initialization of views again as suggested by laalto or

 String matrix_id = ((TextView) view.findViewById(R.id.matrix_id)).getText()
                .toString();

 String name = ((TextView) view.findViewById(R.id.name)).getText()
                .toString();

OTHER TIPS

The item that was clicked is a child of the listview. You don't find the listview by looking at the children of the item here:

String matrix_id = ((ListView) view.findViewById(R.id.list)).getText()
                .toString();

One way to pass ids around is to setTag() the views in adapter getView() and call getTag() to retrieve it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top