我在片段中使用ListView,并按照Android的说明设置了所有必要的要求,我的XML列表代码在下面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#253453">
</ListView>
</LinearLayout>
在我的代码中
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View android = inflater.inflate(R.layout.pending_frag, container, false);
context = getActivity().getApplicationContext();
//ListView list = (ListView)android.findViewById(R.id.list);
ListView list = getListView();
trackItems = new ArrayList<TrackItem>();
listAdapter = new TrackOrderListAdapter(getActivity(), trackItems);
list.setAdapter(listAdapter); }
但是,我仍然会收到此错误
08-20 23:26:26.053:E / AndroidRuntime(30334):致命异常:主要 08-20 23:26:26.053:E / AndroidRuntime(30334):流程: com.deliveryscience.track,PID:30334 08-20 23:26:26.053: E / AndroidRuntime(30334):java.lang.IllegalStateException:内容视图 尚未创建08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.ListFragment.getListView(ListFragment.java:222) 08-20 23:26:26.053:E / AndroidRuntime(30334):at com.deliveryscience.track.PendingActivity.onCreateView(PendingActivity.java:51) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) 08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.view.ViewPager.populate(ViewPager.java:919)08-20 23:26:26.053:E / AndroidRuntime(30334):at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
答案 0 :(得分:1)
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.pending_frag, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
context = getActivity().getApplicationContext();
//ListView list = (ListView)android.findViewById(R.id.list);
ListView list = getListView();
trackItems = new ArrayList<TrackItem>();
listAdapter = new TrackOrderListAdapter(getActivity(), trackItems);
list.setAdapter(listAdapter);
}
在创建视图之前,您无法真正操纵视图。