【问题标题】:ListFragment does not accept my layoutListFragment 不接受我的布局
【发布时间】:2012-05-23 10:19:50
【问题描述】:

根据此链接:ListFragment android developers

我想为列表设置自定义布局,但它有例外。

代码如下:

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;
        }
    }
}}

我的自定义布局是R.layout.topoffers

Logcat:

 05-15 21:43:56.975: W/dalvikvm(218): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
 05-15 21:43:56.975: E/AndroidRuntime(218): Uncaught handler: thread main exiting due to uncaught exception
 05-15 21:43:56.995: E/AndroidRuntime(218): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.anddev.android.ikiwi/org.anddev.android.ikiwi.ListFragmentActivity}:
    java.lang.IllegalStateException: Can't be used with a custom content view 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.os.Handler.dispatchMessage(Handler.java:99) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.os.Looper.loop(Looper.java:123) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.main(ActivityThread.java:4363) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at java.lang.reflect.Method.invokeNative(Native Method) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at java.lang.reflect.Method.invoke(Method.java:521) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at dalvik.system.NativeStart.main(Native Method) 
 05-15 21:43:56.995: E/AndroidRuntime(218): Caused by: java.lang.IllegalStateException:
    Can't be used with a custom content view 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:282)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:258)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at org.anddev.android.ikiwi.ListFragmentActivity$myListFragments.onActivityCreated(ListFragmentActivity.java:57)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.Activity.performStart(Activity.java:3723) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
 05-15 21:43:56.995: E/AndroidRuntime(218):     ... 11 more 
 05-15 21:43:57.025: I/dalvikvm(218): threadid=7: reacting to signal 3 
 05-15 21:43:57.025: E/dalvikvm(218): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

【问题讨论】:

  • 这需要比“它产生异常”更具体
  • 对,但我无法确定是什么造成了异常
  • 发布显示错误的 logcat
  • ListFragmentActivity 的第 57 行是哪一行?

标签: android android-listview android-fragments


【解决方案1】:

创建布局文件list_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>

把它放在你的 ListFragment 类中:

public ListView mList;
boolean mListShown;
View mProgressContainer;
View mListContainer;

public void setListShown(boolean shown, boolean animate){
    if (mListShown == shown) {
        return;
    }
    mListShown = shown;
    if (shown) {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
            mListContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
        }
        mProgressContainer.setVisibility(View.GONE);
        mListContainer.setVisibility(View.VISIBLE);
    } else {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
            mListContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
        }
        mProgressContainer.setVisibility(View.VISIBLE);
        mListContainer.setVisibility(View.INVISIBLE);
    }
}
public void setListShown(boolean shown){
    setListShown(shown, true);
}
public void setListShownNoAnimation(boolean shown) {
    setListShown(shown, false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    int INTERNAL_EMPTY_ID = 0x00ff0001;
    View root = inflater.inflate(R.layout.list_content, container, false);
    (root.findViewById(R.id.internalEmpty)).setId(INTERNAL_EMPTY_ID);
    mList = (ListView) root.findViewById(android.R.id.list);
    mListContainer =  root.findViewById(R.id.listContainer);
    mProgressContainer = root.findViewById(R.id.progressContainer);
    mListShown = true;
    return root;
}

现在可以正常使用了:

setListShown(boolean shown);
setListShown(boolean shown, boolean animate);
setListShownNoAnimation(boolean shown);

来源:

http://source-android.frandroid.com/frameworks/base/core/java/android/app/ListFragment.java

http://source-android.frandroid.com/frameworks/base/core/res/res/layout/list_content.xml

【讨论】:

    【解决方案2】:

    这可能更容易: 像往常一样创建布局,但不要添加列表/空/加载内容。然后在你的 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;
    }
    

    现在你可以使用 setListShown...

    【讨论】:

    • 刚刚试了,可以的。最佳解决方案在这里!比另一种简单得多,因为您可以让 SDK 中的所有方法正常工作。
    • 您不仅可以使用 setListShow(),还可以获得所有库存行为并在您自己的更大布局范围内查看。您可能希望在父级中放置一个容器并将超级视图添加到它而不是父级本身。
    • 这行得通,但正如您所见here,它会导致您的层次结构有两个 android:id/list。
    • +10 亿。我需要这个解决方案来解决不同的问题。我需要在ListFragment 中使用的ListView 上方和下方有自己的额外视图。我想保留他们的ListView,因为我想保留进度条/空的 TextView 功能,但我还需要片段中的一些其他视图,而不仅仅是一个 ListView。这种方法允许我将他们的整个布局放到我自己的布局中。玩了一些,但我能够将它放到我的 LinearLayout 中间的一个空 FrameLayout 中。无论出于何种原因,我都无法像您那样直接添加视图,而我的布局看起来不正确。
    • 还有 Android doc's (scroll to onCreateView()) 建议类似的东西,你 &lt;include&gt; list_content 布局(默认布局的资源 ID - 可以在您的 XML 中的 Android SDK 的 /res 文件夹中找到。但这仅适用于 API 11+,而这种方法应该与 Support-V4 库的 ListFragment 向后兼容回到 API 4。赞一个,我不会想到这样做。
    【解决方案3】:

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

        @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;
    }
    

    【讨论】:

    • 感谢从层次结构中删除不必要的列表视图。
    【解决方案4】:

    这听起来像是已向 Google 报告的问题。

    Google Issue

    如果这确实是您的问题,那么在该页面底部有一些修复它的建议(嗯,使其工作比修复它更重要)。

    【讨论】:

    • 谢谢巴拉克先生,但在他短期内写的这个建议中,我只能看到覆盖 setEmptyText(String)::void 并手动处理空视图.. ..... 我怎么能覆盖 setEmptyText 函数?
    • 我相信你只是从你的布局中删除了 textview 并在你的 onActivityCreated 中放了setEmptyText("Your emptyText message")。我发现的所有示例都表明在您创建/设置适配器之前使用它。
    • 我按照你说的做了,但不幸的是它不起作用,它仍然会导致异常。
    • 它在默认布局下正常工作,但我仍然无法应用我的布局 forlist
    • 我找到了导致问题的原因,但我无法解决,问题出在 setListShown(boolean) 中,当我删除它时,它工作正常,但我需要它,因为我在加载数据时实现了 AsyncTaskLoader所以需要在加载数据时隐藏列表,因为进度条会出现
    【解决方案5】:

    今天我遇到了同样的问题。在谷歌中寻找解决方案,我在一个日本博客中找到了一篇文章。 (http://blog.nkzn.net/entry/2012/06/14/160706)。

    要使用setListShown(boolean),你必须首先在你的布局中做:

    <?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>
    

    现在在您的 ListFragment 中:

    public class SampleListFragment extends ListFragment {
    
      /** ID for progressbar parent */
      private static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002;
      /** ID for listview parent */
      private static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003;
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_list_layout, container, false);
    
        // get you progressBar and his parent
        ProgressBar pBar = (ProgressBar) view.findViewById(android.R.id.progress);
        LinearLayout pframe = (LinearLayout) pBar.getParent();
        pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    
        ListView listView = (ListView) view.findViewById(android.R.id.list);
        listView.setItemsCanFocus(false);
        FrameLayout lFrame = (FrameLayout) listView.getParent();
        lFrame.setId(INTERNAL_LIST_CONTAINER_ID);    
    
        return view;
      } 
    }
    

    在他的父母 setListShown(boolean) 中设置的这 2 个 ids 数字将毫无问题地工作。

    【讨论】:

    • 我阅读了 ListFragment,这是唯一的方法。谢谢 =)
    【解决方案6】:

    另一个对我也有用的解决方案就是把这条线

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

    【讨论】:

      【解决方案7】:

      我只需要更改我的空文本 TextView 属性并这样做

      @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);
          }
      }
      

      不要忘记,如果你在调用setEmptyText("Empty") 之前尝试初始化tvEmpty,你会遇到NullPointerException

      【讨论】:

        【解决方案8】:

        这段代码对我有用:

        @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;
        }
        

        【讨论】:

          【解决方案9】:

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

          Fragment

              @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_list_activity.xml 中,您需要为您的视图设置ID android:id="@id/loading"android:id="@id/content"android:id="@id/empty"

          【讨论】:

            【解决方案10】:

            尝试删除setListShown(true);

            我有同样的问题,前段时间。 如果您在ListFragment 中有自定义布局,则无法使用它的接缝。

            【讨论】:

            • 但我想使用 setListSHown 来控制是否显示列表。我想让它在等待初始数据显示时不显示。在此期间,将显示一个不确定的进度指示器。进度指示器会很好。
            猜你喜欢
            • 1970-01-01
            • 2013-04-27
            • 2012-07-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-24
            • 2018-08-03
            相关资源
            最近更新 更多