ActionBarSherlock:输出类<unknown> </unknown>时出错

时间:2012-08-27 02:08:33

标签: android xml android-layout actionbarsherlock

我正在开发一个使用ActionBarSherlock的应用。该应用程序在SDK v15(ICS)上运行时运行正常,但与SDK v8(Froyo)崩溃!

这是我得到的错误:

E/AndroidRuntime(  489): FATAL EXCEPTION: main
E/AndroidRuntime(  489): android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
E/AndroidRuntime(  489):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
E/AndroidRuntime(  489):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime(  489):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
E/AndroidRuntime(  489):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
E/AndroidRuntime(  489):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime(  489):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime(  489):    at com.finerrecliner.HeadlineAdapter.getView(HeadlineAdapter.java:44)
E/AndroidRuntime(  489):    at android.widget.AbsListView.obtainView(AbsListView.java:1315)
E/AndroidRuntime(  489):    at android.widget.ListView.makeAndAddView(ListView.java:1727)
E/AndroidRuntime(  489):    at android.widget.ListView.fillDown(ListView.java:652)
E/AndroidRuntime(  489):    at android.widget.ListView.fillFromTop(ListView.java:709)
E/AndroidRuntime(  489):    at android.widget.ListView.layoutChildren(ListView.java:1566)
E/AndroidRuntime(  489):    at android.widget.AbsListView.onLayout(AbsListView.java:1147)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1388)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
E/AndroidRuntime(  489):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
E/AndroidRuntime(  489):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  489):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  489):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
E/AndroidRuntime(  489):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/AndroidRuntime(  489):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  489):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  489):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  489):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  489):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  489):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  489):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  489):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  489): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(  489):    at android.widget.TextView.<init>(TextView.java:321)
E/AndroidRuntime(  489):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime(  489):    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
E/AndroidRuntime(  489):    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
E/AndroidRuntime(  489):    ... 39 more
E/AndroidRuntime(  489): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
E/AndroidRuntime(  489):    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
E/AndroidRuntime(  489):    at android.view.View.<init>(View.java:1891)
E/AndroidRuntime(  489):    at android.widget.TextView.<init>(TextView.java:327)
E/AndroidRuntime(  489):    ... 43 more

以下是相关的XML问题(第10行是声明第一个TextView时):

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="#000000"
        android:gravity="center_vertical"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/source_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#666666"
        android:gravity="center_vertical"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    />
</LinearLayout>

这是相关的我的HeadlineAdapter.java代码(第44行注释了注释):

class HeadlineAdapter extends ArrayAdapter<SyndEntryEnhanced> {
    private int mLayoutResourceID;
    private Context mContext;
    private class HeadlineHolder {
        TextView title;
        TextView source_date;
    }

    public HeadlineAdapter(Context context, int textViewResourceId, ArrayList<SyndEntryEnhanced> list) {
        super(context, textViewResourceId, list);
        this.mContext = context;
        this.mLayoutResourceID = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        HeadlineHolder holder = null;

        if (row == null) {
            LayoutInflater infl = (LayoutInflater)((SherlockFragmentActivity)mContext).getLayoutInflater();
            row = infl.inflate(mLayoutResourceID, parent, false);  // LINE 44

            holder = new HeadlineHolder();
            holder.title = (TextView)row.findViewById(R.id.title);
            holder.source_date = (TextView)row.findViewById(R.id.source_date);
            row.setTag(holder);
        } else {
            holder = (HeadlineHolder)row.getTag();
        }

        SyndEntry entry = super.getItem(position).getEntry();
        String title = super.getItem(position).getFeedTitle().trim();
        Date date = entry.getPublishedDate();
        SimpleDateFormat formatter = new SimpleDateFormat("MMM d", Locale.US);

        holder.title.setText(StringEscapeUtils.unescapeHtml(entry.getTitle()));
        if (date == null) {
            holder.source_date.setText(title);
        } else {
            holder.source_date.setText(title + " - " + formatter.format(date));
        }

        return row;
    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

更好的答案是不完整的。您可以将?listPreferredItemPaddingLeft?listPreferredItemPaddingRight与ActionBarSherlock一起使用,因为主题定义了这些属性。另外,将填充定义移到LinearLayout会更有意义,还是打算添加额外的填充?

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="#000000"
        android:gravity="center_vertical"
        android:paddingLeft="?listPreferredItemPaddingLeft"
        android:paddingRight="?listPreferredItemPaddingRight" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/source_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#666666"
        android:gravity="center_vertical"
        android:paddingLeft="?listPreferredItemPaddingLeft"
        android:paddingRight="?listPreferredItemPaddingRight" />
</LinearLayout>

答案 1 :(得分:2)

我明白了。属性listPreferredItemPaddingLeftlistPreferredItemPaddingRight仅在API v14及更高版本中可用。从我的TextViews中删除这些填充属性可以在旧版本的Android上运行时消除错误。

源: http://developer.android.com/reference/android/R.attr.html#listPreferredItemPaddingLeft