在xml中引用当前主题的标记会导致强制关闭

时间:2010-10-27 17:28:33

标签: android

我现在正在为我的应用程序实现主题,而且一切都很顺利,直到最近。我一直在使用android:background =“?thin_border”标签为线性布局提供背景,其中包含我想要在视觉上组合在一起的项目。我遇到的问题是当我尝试在xml中使用此标记时,将自定义类作为其中一个元素,并尝试添加此背景标记。

我有一个看起来像这样的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<com.atraudes.CustomLayout1 xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:layout_gravity="fill_vertical">
    <LinearLayout android:orientation="vertical" 
        android:layout_height="fill_parent"
        android:layout_weight="0" 
        android:background="?thin_border" 
        android:layout_width="wrap_content" />
    ...
</com.atraudes.CustomLayout1>

,自定义类有两个构造函数:

public class CustomLayout1 extends LinearLayout {
    public CustomLayout1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLayout1(Context context) {
        super(context);
    }
    ...
}

使用一些其他函数来存储值,操作数据等。自定义布局类只能直接在xml中调用,而xml会像这样膨胀:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (CustomLayout1) inflater.inflate(R.layout.customlayout1, null);

我遇到的问题是我收到这些错误消息:

10-27 17:13:15.312: ERROR/AndroidRuntime(4281): Uncaught handler: thread main exiting due to uncaught exception
10-27 17:13:15.351: ERROR/AndroidRuntime(4281): android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.createView(LayoutInflater.java:512)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at com.atraudes.CustomListAdapter.getView(CustomListAdapter.java:97)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.AbsListView.obtainView(AbsListView.java:1269)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.ListView.makeAndAddView(ListView.java:1623)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.ListView.fillDown(ListView.java:607)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.ListView.fillFromTop(ListView.java:664)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.ListView.layoutChildren(ListView.java:1481)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.Gallery.setUpChild(Gallery.java:813)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.Gallery.makeAndAddView(Gallery.java:757)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.Gallery.layout(Gallery.java:627)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.Gallery.onLayout(Gallery.java:339)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.layout(View.java:6133)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.ViewRoot.performTraversals(ViewRoot.java:929)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.os.Looper.loop(Looper.java:123)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.app.ActivityThread.main(ActivityThread.java:3948)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at java.lang.reflect.Method.invoke(Method.java:521)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at dalvik.system.NativeStart.main(Native Method)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281): Caused by: java.lang.reflect.InvocationTargetException
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.widget.LinearLayout.<init>(LinearLayout.java:92)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.LayoutInflater.createView(LayoutInflater.java:499)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     ... 48 more
10-27 17:13:15.351: ERROR/AndroidRuntime(4281): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/divider_horizontal_dark.9.png from drawable resource ID #0x0
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.content.res.Resources.loadDrawable(Resources.java:1641)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.<init>(View.java:1725)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.View.<init>(View.java:1674)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.view.ViewGroup.<init>(ViewGroup.java:271)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     ... 52 more
10-27 17:13:15.351: ERROR/AndroidRuntime(4281): Caused by: java.io.FileNotFoundException: res/drawable/divider_horizontal_dark.9.png
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.content.res.AssetManager.openNonAssetNative(Native Method)
10-27 17:13:15.351: ERROR/AndroidRuntime(4281):     at android.c

错误似乎是由android:background =“?thin_border”标签引起的。如果我从布局中删除它,它膨胀很好,程序继续。一旦我重新加入,我就会收到这些错误并且程序停止。

我会非常感激您可能提出的任何意见。谢谢!

2 个答案:

答案 0 :(得分:2)

在过去的一天(比过去更长的时间)把头发拉过来之后,我发现了问题。我在setContent()之前设置了活动的主题,它正在为活动应用主题。我终于想通了在上下文中运行setTheme()以及修复问题。上下文保存主题信息,在通货膨胀时传递给自定义视图。我假设在活动上设置主题也是在上下文中设置: - /

int customTheme = R.style.customTheme;
setTheme(customTheme);
getApplicationContext().setTheme(customTheme);
setContentView(R.layout.mainactivitylayout);

我做的另一件事就是在通货膨胀期间:

v = (CustomLayout1) inflater.inflate(R.layout.customlayout1, parent, false);

父级是父级布局。这是在ListView适配器中完成的,所以就在那里: - )

答案 1 :(得分:0)

应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<com.atraudes.CustomLayout1 xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal" 
   android:layout_height="wrap_content" 
   android:layout_width="fill_parent"
   android:layout_gravity="fill_vertical">
<LinearLayout android:orientation="vertical" 
    android:layout_height="fill_parent"
    android:layout_weight="0" 
    style="@styles/thin_border"
    android:layout_width="wrap_content" />

你的风格应该是

<style name="thin_boarder>
 <item name="android:background">@drawable/customstyle_button_editing</item>
</style>