Android ListFragment不接受我的布局

Android ListFragment不接受我的布局,android,android-listview,android-fragments,Android,Android Listview,Android Fragments,根据这个链接: 我想为列表设置自定义布局,但它会出现例外 代码如下: public class ListFragmentActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); FragmentMa

根据这个链接:

我想为列表设置自定义布局,但它会出现例外

代码如下:

public class ListFragmentActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    FragmentManager fm = getSupportFragmentManager();
    if(fm.findFragmentById(android.R.id.content) == null)
    {
        myListFragments list = new myListFragments();
        fm.beginTransaction().add(android.R.id.content, list).commit();
    }
}

public static class myListFragments extends ListFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.topoffers, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        List<OfferModel> offers = Global.getInstance().getOffers();
        List<OfferModel> topOffers = new ArrayList<OfferModel>(4);
        for (int i = 0; i < 4; i++) {
            if (offers.get(i).getOfferImage() == null)
                offers.get(i).setOfferImage(
                        downloadFile(offers.get(i).getOfferImageUrl()));
            topOffers.add(offers.get(i));
        }
        LazyAdapter adapter = new LazyAdapter(getActivity());
        adapter.setData(topOffers);
        setListAdapter(adapter);
        setListShown(true);
    }

    public Bitmap downloadFile(String fileUrl) {
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(fileUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            return BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}}

这听起来像是一个已经向谷歌报告的问题


如果这确实是您的问题,那么在该页面底部有几条建议可以解决它(好吧,与其说是解决问题,不如说是解决问题)。

尝试删除
setlistShowed(true)

不久前,我也有同样的问题。
如果在
ListFragment
中有自定义布局,则无法使用它创建布局文件list\u content.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <LinearLayout android:id="@+id/progressContainer"
            android:orientation="vertical"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:visibility="gone"
            android:gravity="center">
        
        <ProgressBar style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text=""
                android:paddingTop="4dip"
                android:singleLine="true" />
            
    </LinearLayout>
        
    <FrameLayout android:id="@+id/listContainer"
            android:layout_width="match_parent" 
            android:layout_height="match_parent">
            
        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />
        <TextView android:id="@+id/internalEmpty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    </FrameLayout>
        
</FrameLayout>
现在您可以正常使用:

setListShown(boolean shown);
setListShown(boolean shown, boolean animate);
setListShownNoAnimation(boolean shown);
资料来源:


只需在onCreateView方法中用CustomListView布局替换原始ListView即可。为我工作

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View layout = super.onCreateView(inflater, container,
            savedInstanceState);
    ListView lv = (ListView) layout.findViewById(android.R.id.list);
    ViewGroup parent = (ViewGroup) lv.getParent();

    // Remove ListView and add CustomView  in its place
    int lvIndex = parent.indexOfChild(lv);
    parent.removeViewAt(lvIndex);
    LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.custom_list, container, false);
    parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());
    return layout;
}

我只需要更改我的空文本
TextView
properties就可以了

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    // Swap the new cursor in. (The framework will take care of closing the
    // old cursor once we return)
    mAdapter.swapCursor(cursor);
    if (cursor.getCount() == 0) {
        setEmptyText(getString(R.string.no_users));
        TextView tvEmpty = (TextView) getListView().getEmptyView();
        tvEmpty.setTextColor(getResources().getColor(R.color.blue));
    }
    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}
@覆盖
public void onLoadFinished(加载器,光标){
//在中交换新光标。(框架将负责关闭
//返回后的旧光标)
mAdapter.swapCursor(光标);
if(cursor.getCount()==0){
setEmptyText(getString(R.string.no_用户));
TextView tvEmpty=(TextView)getListView().getEmptyView();
tveempty.setTextColor(getResources().getColor(R.color.blue));
}
//现在应显示该列表。
如果(isResumed()){
SetListShowed(真);
}否则{
SetListShownAnimation(真);
}
}

不要忘记,如果在调用
setEmptyText(“Empty”)
之前尝试初始化
tvEmpty
,您将遇到
NullPointerException

今天我遇到了同样的问题。在谷歌上寻找解决方案,我在一个日本博客上找到了一篇文章。()

要使用
setListShowed(布尔值)
必须首先在布局中执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
    </FrameLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:visibility="gone" >

    <ProgressBar
        android:id="@android:id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>
有了这两个ID,在他的父母SetListShowed(布尔值)中设置的数字将不会出现问题。

这可能更容易: 像平常一样创建布局,但不要添加列表/空/加载内容。然后在listfragment的onCreateView中:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = super.onCreateView(inflater, container, savedInstanceState);
    ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.mylayout, container, false);
    parent.addView(v, 0);
    return parent;
}

现在您可以使用SetListShowed…

这段代码对我很有用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = super.onCreateView(inflater, container, savedInstanceState);

    ((TextView)root.findViewById(0x00ff0001)).setText("some text");

    ListView list = (ListView) root.findViewById(android.R.id.list);
    list.setEmptyView(root.findViewById(0x00ff0001));

    return root;
}

另一个对我同样有效的解决方案就是把这条线

<include layout="@android:layout/list_content" />

而不是列表视图

您可以在其上构建自己的内容,或使用其他视图围绕它。例如,我的布局如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/default_view_padding"
        android:gravity="center"
        android:weightSum="4">

    <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnCounterBack"
            android:src="?attr/ic_previous"
            android:background="?android:attr/selectableItemBackground"
            android:contentDescription="@null"/>

    <TextView
            android:id="@+id/tvCounterLevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/counter_categories"
            android:layout_gravity="center"
            android:paddingTop="@dimen/default_view_padding"
            android:paddingBottom="@dimen/default_view_padding"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:gravity="center"/>

</RelativeLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:background="?attr/dropShadowBackground"
        android:orientation="vertical" >

    <include layout="@android:layout/list_content" />

</LinearLayout>


有点晚了,但没有看到这个答案

内部
片段

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

        ListFragmentHelper.adaptView(view);

        return view;
    }

但是请记住
fragment\u list\u activity.xml
中需要为您的视图设置id
android:id=“@id/load”
android:id=“@id/content”
android:id=“@id/empty”

这需要比“it makes exceptions”更具体,对吧,但我无法确定什么是makes exceptions Post您的日志显示错误,这是ListFragmentActivity中的第57行?谢谢您,巴拉克先生,但在他在短期内写的这个建议中,我只能看到覆盖setEmptyText(字符串)::作废并手动处理空视图。。。。。如何重写setEmptyText函数?我相信您只需从布局和onActivityCreated put中删除textview即可
setEmptyText(“您的emptyText消息”)
。我发现的所有示例都表明,在创建/设置适配器之前使用了。我按照您所说的做了,但不幸的是,它不起作用,仍然会导致异常。它在默认布局下正常工作,但我仍然无法应用我的布局ForList我找到了导致问题的原因,但无法解决问题,问题出现在SetListShowed中(布尔值),当我删除它时,它可以正常工作,但我需要它,因为我在加载数据时实现了AsyncTaskLoader,所以需要在加载数据时将列表隐藏,就像进度条显示的那样。但是我想使用SetListShowed,这样我可以控制列表是否显示。我想在等待初始数据时使其不显示a将在其中显示。在此期间,将显示一个不确定的进度指示器。prgress指示器将很好。只要尝试,它就可以工作。这里的最佳解决方案!比其他方法简单得多,因为您可以让SDK中的所有方法都正常工作。不仅可以使用setListShow(),但您可以获得所有的股票行为,并在自己更大的布局范围内进行查看。您可能希望在父级中放置一个容器,并将超级视图添加到其中,而不是添加到父级本身。这是可行的,但正如您所见,它会导致您的层次结构有两个android:id/list.+10亿。我需要此解决方案来解决另一个问题m、 我需要在
ListFragment
中使用的
ListView
的上方和下方添加我自己的额外视图。我想保留它们的
ListView
,因为我想保留进度条/空文本视图功能,但我还需要片段中的一些其他视图,而不仅仅是列表视图。这种方法允许我删除它们的整个布局在我自己的周围,但我可以把它放在我的线性布局中间的一个空框架中。无论什么原因,我都不能直接添加视图,就像你没有我的布局看起来错误一样。也可以(滚动到OnCeCaleVIEW())建议类似的东西,在那里你可以
列出内容
布局(默认布局的资源ID-可以在Android SDK的/res文件夹中找到)但这只适用于API 11+,而这种方法应该与Support-V4库的
ListFragment
向后兼容,返回到API 4。感谢你,我没有想到要这么做。感谢你从层次结构中删除了不必要的列表视图。我阅读了ListFragment,这是唯一的方法。。谢谢=)
<include layout="@android:layout/list_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/default_view_padding"
        android:gravity="center"
        android:weightSum="4">

    <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnCounterBack"
            android:src="?attr/ic_previous"
            android:background="?android:attr/selectableItemBackground"
            android:contentDescription="@null"/>

    <TextView
            android:id="@+id/tvCounterLevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/counter_categories"
            android:layout_gravity="center"
            android:paddingTop="@dimen/default_view_padding"
            android:paddingBottom="@dimen/default_view_padding"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:gravity="center"/>

</RelativeLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:background="?attr/dropShadowBackground"
        android:orientation="vertical" >

    <include layout="@android:layout/list_content" />

</LinearLayout>
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_list_activity,
                container, false);  

        ListFragmentHelper.adaptView(view);

        return view;
    }