我的listview适配器和Listener出现问题。我是Android的新手,我想我正确设置了它。我希望我的onclicklistner只显示所选列表项的名称。最终我希望它做得更多。每当我尝试运行此选项卡时,我都会关闭一个力量。这让我相信onclicklistner之前存在一个问题。我是否错误地构造了SherlockList Fragment?
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)
我的listview在我的main.xml中定义
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
答案 0 :(得分:2)
你有这个,你的班级延伸SherLockListFragment
。
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
将ListView初始化为
ListView listView = getListView();
答案 1 :(得分:1)
更改xml文件中listview的id,如android:id =“@ android:id / list”,更改java文件也就像,ListView listView =(ListView)(herbView.findViewById(android.R.id.list) ));