为什么我在lv = getListView();
上获得空指针?
public class bListFragment extends ListFragment {
static public final String STATE_CHECKED="com.tyczj.bowling.STATE_CHECKED";
OnLongListClickListener mListener;
ListView lv;
String name;
long id;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lv = getListView(); //NullPointerException
lv.setTextFilterEnabled(true);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Vibrator v = (Vibrator)getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
v.vibrate(new long [] {0,20,0,0}, -1);
TextView tv = (TextView)arg1.findViewById(R.id.bListTextView);
name = tv.getText().toString();
try{
id = Integer.parseInt(getRow(name));
mListener.OnLongListClick(id);
}catch(CursorIndexOutOfBoundsException e){
populateList();
}
return false;
}
});
}
logcat错误
11-22 12:48:39.170: E/AndroidRuntime(14259): FATAL EXCEPTION: main
11-22 12:48:39.170: E/AndroidRuntime(14259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.bowling/com.tyczj.bowling.Tabs}: java.lang.NullPointerException
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.os.Looper.loop(Looper.java:132)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread.main(ActivityThread.java:4028)
11-22 12:48:39.170: E/AndroidRuntime(14259): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:48:39.170: E/AndroidRuntime(14259): at java.lang.reflect.Method.invoke(Method.java:491)
11-22 12:48:39.170: E/AndroidRuntime(14259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-22 12:48:39.170: E/AndroidRuntime(14259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-22 12:48:39.170: E/AndroidRuntime(14259): at dalvik.system.NativeStart.main(Native Method)
11-22 12:48:39.170: E/AndroidRuntime(14259): Caused by: java.lang.NullPointerException
11-22 12:48:39.170: E/AndroidRuntime(14259): at com.tyczj.bowling.BowlersListFragment.onCreate(BowlersListFragment.java:29)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:702)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.BackStackRecord.run(BackStackRecord.java:578)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1226)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.Activity.performStart(Activity.java:4370)
11-22 12:48:39.170: E/AndroidRuntime(14259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1724)
答案 0 :(得分:2)
查看documentation for ListActivity。
首先需要一个布局文件(xml文件),其中包含ListView
,其ID为android:id="@android:id/list"
。
其次,您忘记设置Activity
的内容视图:
super.onCreate(savedInstanceState);
this.setContent(R.layout.yourLayoutWithListView);
答案 1 :(得分:0)
你把com.tyczj.bowling / com.tyczj.bowling.Tabs活动放在清单文件中了吗?
答案 2 :(得分:0)
可能是不正确但是你错过了setContent吗?
super.onCreate(savedInstanceState);
??? you must set content XML
lv = getListView(); //NullPointerException
这就像使用TabActivity一样 - 只有在设置了内容XML布局定义之后才能调用getTabHost。 尝试
super.onCreate(savedInstanceState);
**this.setContent(R.layout.mylistviewlayout);**
lv = getListView(); //NullPointerException
答案 3 :(得分:0)
如果您使用的是ListFragment,则可能需要将onCreate更改为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
然后
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv = getListView(); // here it is available
}