在片段中创建视图时出错

时间:2013-06-16 09:40:47

标签: java android eclipse fragment

我创建了一个带有3片段布局的活动,用户将滑动屏幕以显示另一个片段。

在我的第一个片段上,我计划让ListView显示用户的信息。 但是,我通常用于扩展活动的代码不适用于extends Fragment。 我研究了一下,最后摆脱了红线,但是当我显示片段时我的应用程序崩溃了。

这是我的代码:

public class LayoutOne extends ListFragment {
ListView myInfoList;
String[] items = {"Item 1", "Item 2"};
ArrayAdapter<CharSequence> adapter;

public static Fragment newInstance(Context context) {
    LayoutOne f = new LayoutOne();  

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_one, container, false);    

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    myInfoList = getListView();
    adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
    myInfoList.setAdapter(adapter);
}


}

这是我的日志猫:

  

06-16 17:57:16.150:D / AndroidRuntime(20378):关闭VM   06-16 17:57:16.150:W / dalvikvm(20378):threadid = 1:线程退出未捕获异常(组= 0x40ff9930)   06-16 17:57:16.158:E / AndroidRuntime(20378):致命异常:主要   06-16 17:57:16.158:E / AndroidRuntime(20378):java.lang.RuntimeException:您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.view.ViewPager.populate(ViewPager.java:804)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.View.measure(View.java:15523)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.View.measure(View.java:15523)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.View.measure(View.java:15523)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.LinearLayout.measureVertical(LinearLayout.java:847)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.View.measure(View.java:15523)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)   06-16 17:57:16.158:E / AndroidRuntime(20378):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2434)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.View.measure(View.java:15523)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4351)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.Choreographer.doCallbacks(Choreographer.java:562)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.Choreographer.doFrame(Choreographer.java:532)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.os.Handler.handleCallback(Handler.java:725)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.os.Handler.dispatchMessage(Handler.java:92)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.os.Looper.loop(Looper.java:137)   06-16 17:57:16.158:E / AndroidRuntime(20378):在android.app.ActivityThread.main(ActivityThread.java:5233)   06-16 17:57:16.158:E / AndroidRuntime(20378):at java.lang.reflect.Method.invokeNative(Native Method)   06-16 17:57:16.158:E / AndroidRuntime(20378):at java.lang.reflect.Method.invoke(Method.java:511)   06-16 17:57:16.158:E / AndroidRuntime(20378):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:799)   06-16 17:57:16.158:E / AndroidRuntime(20378):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)   06-16 17:57:16.158:E / AndroidRuntime(20378):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你永远不会初始化myInfoList。 Ihmo你的类应该扩展ListFragment而不是Fragment。

你的onCreateView应该膨胀包含ListView的布局,你应该移动

adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
myInfoList.setAdapter(adapter); 

在onActivityCreate中:

public void onActivityCreated(Bundle savedInstanceState) {
    myInfoList = getListView();
    adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
    myInfoList.setAdapter(adapter); 
}

ListView的ID必须是

android:id="@id/android:list"