Android的ListFragment是混乱 [英] Android ListFragment is to confusing

查看:103
本文介绍了Android的ListFragment是混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑使用ListFragment ......我不能明白问题出在哪里?

I'm so confused using ListFragment... I cant understand where is the problem...

下面是我的FragmentActivity类:

Here is my FragmentActivity class:

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类:

My ListFragment class:

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中(它是用于制造列表,自定义列表适配器我做了):

Also main.xml (it's used for making list with custom list adapter i made):

<?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应显示在顶部的背景图像和列表的片段:

This root_menu.xml should show an background image and list fragment on the top:

<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类:

This is my CustomListAdapter class:

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

我看到它说,不创建内容查看,但我不明白为什么...

I see it says that contentview is not created but i dont understand why...

推荐答案

那么,从logcat的:

Well, from the 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之前。

You are trying to get the listview before you set the content view. Move the view inflation to before the getListView.

我相信这也将解决有关充气类片段的第二个错误。

I believe this will also fix the second error about inflating the class fragment.

这篇关于Android的ListFragment是混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆