我在Fragment类中使用ListView小部件。不在ListFragment中,因为我在同一个片段中使用其他小部件。因此,使用ListFragment类将使ListView占据整个屏幕。 但是,使用ListView类的实例,“setAdapter()”会导致应用程序崩溃。这是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] systems = {"IOS","Android","Windows","Mac","Linux"};
ListView lv = (ListView) getActivity().findViewById(R.id.mainList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, systems);
lv.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
return rootView;
}
当我包装“lv.setAdapter(adapter);”时在try-catch块中,应用程序不会崩溃。这就是我认为这就是问题的原因。
这是Fragment的布局代码:
<?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:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/mainList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/darkbg"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/butEdit"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="@string/delete_button"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/butOpen"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="@string/open_button"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
这是activity_main.xml代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainActivityContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.my.app.MainActivity" >
<fragment
android:id="@+id/listFragment"
android:name="org.my.app.fragments.MyListFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:layouts="@layout/fragment_list" />
</FrameLayout>
答案 0 :(得分:1)
onCreate
之前调用 onCreateView
,因此片段布局尚未在那里膨胀。
您需要将处理片段布局的代码移动到发生所有通货膨胀的onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
String[] systems = {"IOS","Android","Windows","Mac","Linux"};
ListView lv = (ListView) rootView.findViewById(R.id.mainList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, systems);
lv.setAdapter(adapter);
return rootView;
}