致命的onItemClick侦听器

时间:2014-03-25 06:58:50

标签: java android onitemclicklistener

情况是我选择一个列表项。它将在下一个活动中显示该项目的详细信息。

我尝试使用ListItem侦听器从选定的onItemClick获取值。点击该项目时我得到了error [FATAL EXCEPTION]

请帮帮我。

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

这是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();


}

这个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"/>

这是fragment_schedule.xml

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

这是日志错误。

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)

2 个答案:

答案 0 :(得分:2)

您在布局中没有为适配器充气的列表视图

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

因此,上面的初始化为您提供了NullpointerExcpetion

猜猜工作:

我可以从上一个问题中猜出我回答了你的问题(ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView)。对于使用ID R.id.matrix_idR.id.name在适配器中充气的布局,您有TextView。但要确认您可以发布适配器代码。

复制上一个问题

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

您可以使用getTagsetTag来避免按照laalto或

的建议再次初始化视图
 String matrix_id = ((TextView) view.findViewById(R.id.matrix_id)).getText()
                .toString();

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

答案 1 :(得分:0)

单击的项目是listview的子项。您无法通过查看项目的子项来查找列表视图:

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

传递ID的一种方法是setTag()适配器getView()中的视图,并调用getTag()来检索它。