3

我对使用 ListFragment 感到很困惑......我不明白问题出在哪里......

这是我的 FragmentActivity 类:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class menu extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
            setContentView(R.layout.root_menu); 

        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            ListFrag list = new ListFrag();
            getSupportFragmentManager().beginTransaction().replace(android.R.id.content, list).commit();
        }}}

我的 ListFragment 类:

public class ListFrag extends ListFragment {

    int Files = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        getListView().setBackgroundResource(R.drawable.table);
        getListView().setDividerHeight(0);
        getListView().setCacheColorHint(0);

        View view = inflater.inflate(R.layout.main, null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setListAdapter(new CustomListAdapter(getActivity(), getThemes()));
    }

    public String[] getThemes(){
                       ...
        return ...;
    }

还有main.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="wrap_content"
    android:paddingLeft="40dp"
    android:paddingTop="30dp"
    android:cacheColorHint="#00000000"
    android:layout_marginTop="100px"
     >


    <ImageView
        android:id="@+id/icon"
        android:layout_height="40dp"
        android:layout_width="40dp"
        android:src="@drawable/check" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:paddingTop="6dp"
        android:paddingLeft="20dp"
        android:textSize="25px" >
    </TextView>

</LinearLayout>   

此 root_menu.xml 应在顶部显示背景图像和列表片段:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/back"
android:orientation="vertical"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/back"
android:cacheColorHint="#00000000"
>

<fragment
      android:id="@+id/frag"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.osmgames.kartuves.ListFrag" >
</fragment>   

</LinearLayout>

这是我的 CustomListAdapter 类:

public class CustomListAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public CustomListAdapter(Context context, String[] values) {
        super(context, R.layout.main, values);
            this.context = context;
            this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.main, parent, false);

            TextView textView = (TextView) rowView.findViewById(R.id.label);

                ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

                textView.setText(values[position]);

            imageView.setImageResource(R.drawable.check);

        return rowView;
    }
}

日志:

05-07 11:49:43.017: D/AndroidRuntime(1216): Shutting down VM
05-07 11:49:43.017: W/dalvikvm(1216): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-07 11:49:43.047: E/AndroidRuntime(1216): FATAL EXCEPTION: main
05-07 11:49:43.047: E/AndroidRuntime(1216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.osmgames.kartuves/com.osmgames.kartuves.menu}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.os.Looper.loop(Looper.java:123)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at java.lang.reflect.Method.invoke(Method.java:521)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at dalvik.system.NativeStart.main(Native Method)
05-07 11:49:43.047: E/AndroidRuntime(1216): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.Activity.setContentView(Activity.java:1647)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at com.osmgames.kartuves.menu.onCreate(menu.java:12)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-07 11:49:43.047: E/AndroidRuntime(1216):     ... 11 more
05-07 11:49:43.047: E/AndroidRuntime(1216): Caused by: java.lang.IllegalStateException: Content view not yet created
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at com.osmgames.kartuves.ListFrag.onCreateView(ListFrag.java:22)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
05-07 11:49:43.047: E/AndroidRuntime(1216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
05-07 11:49:43.047: E/AndroidRuntime(1216):     ... 20 more

我看到它说没有创建内容视图,但我不明白为什么......

4

1 回答 1

5

好吧,从logcat:

Caused by: java.lang.IllegalStateException: Content view not yet created

你的问题在这里:

    getListView().setBackgroundResource(R.drawable.table);
    getListView().setDividerHeight(0);
    getListView().setCacheColorHint(0);

    View view = inflater.inflate(R.layout.main, null);

您试图在设置内容视图之前获取列表视图。将视图膨胀移动到 getListView 之前。

我相信这也将解决关于膨胀类片段的第二个错误。

于 2012-05-07T15:08:29.277 回答