0

我的列表视图适配器和侦听器有问题。我对Android有点陌生,我认为我设置正确。我希望我的 onclicklistner 只显示所选列表项的名称。最终,我希望它做得更多。每当我尝试运行此选项卡时,我都会强制关闭。它让我相信在 onclicklistner 之前有一个问题。我是否错误地构建了 SherlockList 片段?

public class HerbsFragment extends SherlockListFragment  implements ActionBar.TabListener{
private View herbView;
/** An array of items to display in ArrayList */
String organs[] = new String[]{
        "Liver",
        "Kidneys",
        "Eyes",
        "Heart",
        "Blood"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //super.onCreateView(inflater, container, savedInstanceState);

    herbView = inflater.inflate(R.layout.main, container, false);
    ListView listView = (ListView)(herbView.findViewById(R.id.listview));

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, organs);
    listView.setAdapter(adapter);

    OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView <? > arg0, View arg1, int position,
            long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getSherlockActivity(), 
                 "You selected :", Toast.LENGTH_SHORT).show();
        }
    };
    listView.setOnItemClickListener(onItemClickListener);

    return herbView;//inflater.inflate(R.layout.main, container, false);
}
@Override
public void onStart() {     
    super.onStart();

    /** Setting the multiselect choice mode for the listview */
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}

我发布了logcat

06-29 23:49:56.911: E/AndroidRuntime(13875): FATAL EXCEPTION: main
06-29 23:49:56.911: E/AndroidRuntime(13875): java.lang.NullPointerException
06-29 23:49:56.911: E/AndroidRuntime(13875):    at in.wptrafficanalyzer.actionbarsherlocknavtab.HerbsFragment.onCreateView(HerbsFragment.java:43)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.os.Handler.handleCallback(Handler.java:587)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.os.Looper.loop(Looper.java:130)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at android.app.ActivityThread.main(ActivityThread.java:3691)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at java.lang.reflect.Method.invoke(Method.java:507)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-29 23:49:56.911: E/AndroidRuntime(13875):    at dalvik.system.NativeStart.main(Native Method)

我的列表视图在我的 main.xml 中定义

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

2 回答 2

2

你有这个并且你的类扩展了SherLockListFragment

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

所以将 ListView 初始化为

 ListView listView = getListView(); 
于 2013-06-30T05:05:24.633 回答
1

在xml文件中更改listview的id,如android:id="@android:id/list",在java文件中更改也如,ListView listView = (ListView)(herbView.findViewById(android.R.id.list));

于 2013-06-30T05:11:01.083 回答