0

嗨,我在片段中遇到了 android gridview 的问题。我有搜索解决方案但没有成功,也许在这里我解决了这个问题。谢谢。我的代码是:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frontpage, container, false);
        createGridCategory(inflater, view);
        return view;
    }

    public void createGridCategory(LayoutInflater inflater, View container) {
        AdapterView<ListAdapter> itemsGridViewObj = (GridView) container.findViewById(R.id.newsScrollerContent);

        categoryNames = database.getEnabledCategories()[1];
        physicalCategories = new ArrayList<RelativeLayout>(categoryNames.length);
        physicalItems = new ItemLayout[categoryNames.length][categoryRowLength]; 

        // loop through adding category views
        for (int i = 0; i < categoryNames.length; i++) {
            RelativeLayout categoryTitleBar = (RelativeLayout) inflater
                    .inflate(R.layout.list_category_item, null);

            categoryTitleBar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    int id = physicalCategories.indexOf(view);
                    clickHandler.onCategoryClick(categoryNames[id]);
                }
            });

            TextView name = (TextView) categoryTitleBar
                    .findViewById(R.id.categoryTitle);
            name.setText(categoryNames[i]);

            physicalCategories.add(i, categoryTitleBar);
            itemsGridViewObj.addView(categoryTitleBar);
        }
    }

/res/layout/frontpage.xml 中的布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/newsScroller"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F5F5" >   

   <GridView
        android:id="@+id/newsScrollerContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp" >
    </GridView>

</ScrollView>

/res/layout/list_category_item.xml 中的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/categoryTitleBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="7dp"
    android:paddingLeft="7dp"
    android:paddingRight="7dp"
    android:paddingTop="7dp" >

    <TextView
        android:id="@+id/categoryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Title"
        android:textSize="17sp" />


</RelativeLayout>

如果我在 logcat 中查看错误是:

01-09 05:02:20.139: E/AndroidRuntime(10804): FATAL EXCEPTION: main
01-09 05:02:20.139: E/AndroidRuntime(10804): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.digitallizard.bbcnewsreader/com.digitallizard.bbcnewsreader.ReaderActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1959)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread.access$600(ActivityThread.java:126)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.os.Looper.loop(Looper.java:137)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread.main(ActivityThread.java:4456)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at dalvik.system.NativeStart.main(Native Method)
01-09 05:02:20.139: E/AndroidRuntime(10804): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.digitallizard.bbcnewsreader.ReaderActivity.onCreate(ReaderActivity.java:450)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.Activity.performCreate(Activity.java:4465)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1923)
01-09 05:02:20.139: E/AndroidRuntime(10804):    ... 11 more
01-09 05:02:20.139: E/AndroidRuntime(10804): Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.widget.AdapterView.addView(AdapterView.java:445)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.digitallizard.bbcnewsreader.fragments.FrontpageFragment.createGridCategory(FrontpageFragment.java:123)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at com.digitallizard.bbcnewsreader.fragments.FrontpageFragment.onCreateView(FrontpageFragment.java:95)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
01-09 05:02:20.139: E/AndroidRuntime(10804):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
01-09 05:02:20.139: E/AndroidRuntime(10804):    ... 21 more

请帮助我,谢谢。

4

1 回答 1

1

您的问题是您正在使用抛出 UnsupportedOperationException 的不受支持的方法。检查这个

您可以覆盖。

于 2013-04-08T20:35:21.667 回答