自定义listView设计在设备上不起作用,但在模拟器中工作

时间:2012-08-13 11:27:29

标签: android motodev-studio

以下是代码:

adapter=new ArrayAdapter<String>(
                this, 
                R.layout.zalistu, 
                android.R.id.text1, 
                values);

所以这是适配器,我在列表中创建了自己的设计(R.layout.zalistu),这在模拟器中工作,但在设备上它给我一个错误(应用程序意外停止)如果我使用simple_list_item_1然后它在模拟器和我的设备上都能正常工作。

这是R:layout.zalistu:

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textStyle="bold"
    android:textColor="#FF00FF"
/>

LogCat:

08-13 13:26:15.909: E/AndroidRuntime(6832): FATAL EXCEPTION: main
08-13 13:26:15.909: E/AndroidRuntime(6832): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.AbsListView.obtainView(AbsListView.java:1495)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.ListView.onMeasure(ListView.java:1127)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.measure(View.java:8335)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.measure(View.java:8335)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.measure(View.java:8335)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.measure(View.java:8335)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.measure(View.java:8335)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1892)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.os.Looper.loop(Looper.java:130)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.app.ActivityThread.main(ActivityThread.java:3835)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at java.lang.reflect.Method.invoke(Method.java:507)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at dalvik.system.NativeStart.main(Native Method)
08-13 13:26:15.909: E/AndroidRuntime(6832): Caused by: java.lang.reflect.InvocationTargetException
08-13 13:26:15.909: E/AndroidRuntime(6832):     at java.lang.reflect.Constructor.constructNative(Native Method)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-13 13:26:15.909: E/AndroidRuntime(6832):     ... 32 more
08-13 13:26:15.909: E/AndroidRuntime(6832): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.view.View.<init>(View.java:1978)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.TextView.<init>(TextView.java:350)
08-13 13:26:15.909: E/AndroidRuntime(6832):     at android.widget.TextView.<init>(TextView.java:343)
08-13 13:26:15.909: E/AndroidRuntime(6832):     ... 35 more

我的设备上的Android版本是2.3.7,模拟器上的版本是4.0.4(API 15)

2 个答案:

答案 0 :(得分:3)

您使用过的R.attr的所有属性,例如listPreferredItemPaddingLeft,仅适用于您的设备为姜饼的Android 14(ICS)。解决方案:手动设置,尝试一些值,直到看起来正确。或者相应地设置minSDK。

答案 1 :(得分:1)

使用 android:id="@+id/text1" 代替 android:id="@android:id/text1"

还有...

<强> adapter=new ArrayAdapter<String>(this,R.layout.zalistu,R.id.text1,values);