将GreenDroid ItemAdapter与常规活动一起使用



我想将GreenDroid的ItemAdapter和自定义列表项与常规ListActivity一起使用(或在SherlockListActivity中的Future中)。我提取了ItemAdapter、Item和View类,并创建了一个自定义项目。(注意:昨天我使用整个GreenDroid项目尝试了同样的方法。所以,我的新提取项目没有问题)

我的示例ListActivity:

public class TestActivity extends ListActivity {
    /** Called when the activity is first created. */
    ItemAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter=new ItemAdapter(this);
        adapter.add(new TextItem("test"));
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);
    }
}

崩溃并给出以下输出:

01-30 17:07:33.983: E/AndroidRuntime(21042): FATAL EXCEPTION: main
01-30 17:07:33.983: E/AndroidRuntime(21042): android.view.InflateException: Binary XML file line #20: Error inflating class com.itusozluk.itemadapter.widget.itemview.TextItemView
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.view.LayoutInflater.createView(LayoutInflater.java:606) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at com.itusozluk.itemadapter.widget.item.Item.createCellFromXml(Item.java:125)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at com.itusozluk.itemadapter.widget.item.TextItem.newView(TextItem.java:61)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at com.itusozluk.itemadapter.ItemAdapter.getView(ItemAdapter.java:445)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.widget.AbsListView.obtainView(AbsListView.java:2033) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.widget.ListView.makeAndAddView(ListView.java:1772) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.widget.ListView.fillDown(ListView.java:672) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.widget.ListView.fillFromTop(ListView.java:732) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.widget.ListView.layoutChildren(ListView.java:1625) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.widget.AbsListView.onLayout(AbsListView.java:1863) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.View.layout(View.java:11278) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.view.ViewGroup.layout(ViewGroup.java:4224) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.View.layout(View.java:11278) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.view.ViewGroup.layout(ViewGroup.java:4224) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
01-30 17:07:33.983: E/AndroidRuntime(21042): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
01-30 17:07:33.983: E/AndroidRuntime(21042): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.View.layout(View.java:11278) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.view.ViewGroup.layout(ViewGroup.java:4224) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.View.layout(View.java:11278) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.view.ViewGroup.layout(ViewGroup.java:4224) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.os.Handler.dispatchMessage(Handler.java:99) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.os.Looper.loop(Looper.java:137) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.app.ActivityThread.main(ActivityThread.java:4424) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at java.lang.reflect.Method.invokeNative(Native Method) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at java.lang.reflect.Method.invoke(Method.java:511) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at dalvik.system.NativeStart.main(Native Method) 01-30 17:07:33.983:
            E/AndroidRuntime(21042): Caused by: java.lang.reflect.InvocationTargetException 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at java.lang.reflect.Constructor.constructNative(Native Method) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 01-30
17:07:33.983: E/AndroidRuntime(21042):  at android.view.LayoutInflater.createView(LayoutInflater.java:586) 01-30
17:07:33.983: E/AndroidRuntime(21042):  ... 35 more 01-30
17:07:33.983: E/AndroidRuntime(21042): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2 
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at android.view.View.<init>(View.java:2791) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at android.widget.TextView.<init>(TextView.java:449) 01-30 17:07:33.983:
            E/AndroidRuntime(21042):    at com.itusozluk.itemadapter.widget.itemview.TextItemView.<init>(TextItemView.java:36)
01-30 17:07:33.983: E/AndroidRuntime(21042):    at com.itusozluk.itemadapter.widget.itemview.TextItemView.<init>(TextItemView.java:32)
01-30 17:07:33.983: E/AndroidRuntime(21042):    ... 38 more

我认为最重要的部分是:

java.lang.UnsupportedOperationException: Can't convert to dimension:
type=0x2 01-30 17:07:33.983: E/AndroidRuntime(21042):

GreenDroid的维度文件中应该有一些内容。

如何修复?没有GDActivity,如何使用GreenDroid的ItemAdapter?

这实际上不是一个GDActivity问题,而是一个主题问题。我目前正在使用一个精简版的GreenDroid和ActionbarSherlock 4。

为了解决您的问题,您需要制作一个parent=theme.Sherlock的主题。不幸的是,您无法添加多个parent,因此您需要将GD主题的内容复制并粘贴到您的自定义主题中。

我的是这样的:theme.xml

<resources>
    <style name="Theme.MyTheme" parent="Theme.Sherlock">
        <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
        <item name="android:actionBarStyle">?actionBarStyle</item>
        <!--  GreenDroid stuff -->
        <item name="gdTextAppearance">@style/TextAppearance</item>
        <item name="gdTextAppearanceLarge">@style/TextAppearance.Large</item>
        <item name="gdTextAppearanceMedium">@style/TextAppearance.Medium</item>
        <item name="gdTextAppearanceSmall">@style/TextAppearance.Small</item>
        <item name="gdTextAppearanceSeparator">@style/TextAppearance.Separator</item>
        <item name="gdDrawableWidth">@dimen/gd_drawable_width</item>
        <item name="gdDrawableHeight">@dimen/gd_drawable_height</item>
        <item name="gdDrawableMargin">@dimen/gd_drawable_margin</item>
        <item name="gdItemViewPreferredHeight">@dimen/gd_item_view_height</item>
        <item name="gdItemViewPreferredHalfHeight">@dimen/gd_item_view_half_height</item>
        <item name="gdItemViewPreferredPaddingLeft">@dimen/gd_item_view_padding_left</item>
        <item name="gdSeparatorItemViewPreferredHeight">@dimen/gd_separator_item_view_height</item>
        <item name="gdTextItemViewStyle">@style/GreenDroid.Widget.ItemView.TextItemView</item>
        <item name="gdLongTextItemViewStyle">@style/GreenDroid.Widget.ItemView.LongTextItemView</item>
        <item name="gdDescriptionItemViewStyle">@style/GreenDroid.Widget.ItemView.DescriptionItemView</item>
        <item name="gdSeparatorItemViewStyle">@style/GreenDroid.Widget.ItemView.SeparatorItemView</item>
        <item name="gdProgressItemViewStyle">@style/GreenDroid.Widget.ItemView.ProgressItemView</item>
        <item name="gdProgressItemViewStyleText">@style/GreenDroid.Widget.ItemView.ProgressItemView.Text</item>
        <item name="gdProgressItemViewStyleProgressBar">@style/GreenDroid.Widget.ItemView.ProgressItemView.ProgressBar</item>
        <item name="gdDrawableItemViewStyle">@style/GreenDroid.Widget.ItemView.DrawableItemView</item>
        <item name="gdDrawableItemViewStyleText">@style/GreenDroid.Widget.ItemView.DrawableItemView.Text</item>
        <item name="gdDrawableItemViewStyleDrawable">@style/GreenDroid.Widget.ItemView.DrawableItemView.Drawable</item>
        <item name="gdSubtitleItemViewStyle">@style/GreenDroid.Widget.ItemView.SubtitleItemView</item>
        <item name="gdSubtitleItemViewStyleText">@style/GreenDroid.Widget.ItemView.SubtitleItemView.Text</item>
        <item name="gdSubtitleItemViewStyleSubtitle">@style/GreenDroid.Widget.ItemView.SubtitleItemView.SubtitleText</item>
        <item name="gdSubtextItemViewStyle">@style/GreenDroid.Widget.ItemView.SubtextItemView</item>
        <item name="gdSubtextItemViewStyleText">@style/GreenDroid.Widget.ItemView.SubtextItemView.Text</item>
        <item name="gdSubtextItemViewStyleSubtext">@style/GreenDroid.Widget.ItemView.SubtextItemView.Subtext</item>
        <item name="gdThumbnailItemViewStyle">@style/GreenDroid.Widget.ItemView.ThumbnailItemView</item>
        <item name="gdThumbnailItemViewStyleText">@style/GreenDroid.Widget.ItemView.ThumbnailItemView.Text</item>
        <item name="gdThumbnailItemViewStyleSubtitle">@style/GreenDroid.Widget.ItemView.ThumbnailItemView.Subtitle</item>
        <item name="gdThumbnailItemViewStyleThumbnail">@style/GreenDroid.Widget.ItemView.ThumbnailItemView.Thumbnail</item>
    </style>
</resources>

确保将活动的主题设置为自定义主题。

如果你介意的话,你可以从https://github.com/croemmich/GreenDroid.我已经删除了ActionBar的所有内容。

相关内容

  • 没有找到相关文章

最新更新