首先,对不起我的英语。膨胀布局时我有一个问题:
我有一个自定义视图,从LinearLayout扩展,名为 ButtonHelp 。我在名为 LoadingActivity 的布局上使用该视图。我的下面的代码在每个设备和模拟器上都能很好地工作,除了像三星Galaxy Y这样的QVGA屏幕之外,还有以下错误:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.LoadingActivity}: android.view.InflateException: Binary XML file line #45: Error inflating class com.myapp.customviews.ButtonHelp
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class com.myapp.customviews.ButtonHelp
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at com.myapp.LoadingActivity.onCreate(LoadingActivity.java:45)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 21 more
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageButton
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.myapp.customviews.ButtonHelp.initLayout(ButtonHelp.java:29)
at com.myapp.customviews.ButtonHelp.<init>(ButtonHelp.java:17)
... 24 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 32 more
Caused by: java.lang.StackOverflowError
at android.util.LongSparseArray.get(LongSparseArray.java:79)
at android.content.res.Resources.getCachedDrawable(Resources.java:1746)
at android.content.res.Resources.loadDrawable(Resources.java:1664)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1694)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.
问题是,我不知道这一行:
Caused by: java.lang.StackOverflowError
at android.util.LongSparseArray.get(LongSparseArray.java:79)
为什么会在QVGA仿真器/设备上发生?
供参考:
ButtonHelp 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btnhelp_help"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/loading_button_help"
android:src="@drawable/loading_icon_help" />
</LinearLayout>
ButtonHelp 代码:
public class ButtonHelp extends LinearLayout {
private ImageButton mHelp;
public ButtonHelp(Context context, AttributeSet attrs) {
super(context, attrs);
initLayout();
initControls();
}
public ButtonHelp(Context context) {
super(context);
initLayout();
initControls();
}
private void initLayout() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.btn_help, this);
}
private void initControls() {
mHelp = (ImageButton) findViewById(R.id.btnhelp_help);
}
public void setOnClickListener(View.OnClickListener listener) {
mHelp.setOnClickListener(listener);
}
}
ButtonHelp 用于 LoadingActivity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loading_background"
android:orientation="vertical" >
...
<com.myapp.customviews.ButtonHelp
android:id="@+id/loading_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
...
</RelativeLayout>
我已经搜索了另外的SO问题,但仍然没有针对此案例的解决方案或解释。
非常感谢!
如上所述,以下是loading_button_help
的代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/loading_button_help" /> <!-- Normal -->
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/loading_button_help" /> <!-- Normal disable -->
<item android:state_pressed="true"
android:drawable="@drawable/loading_button_help_press" /> <!-- pressed -->
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/loading_button_help" /> <!-- selected -->
<item android:state_enabled="true"
android:drawable="@drawable/loading_button_help" /> <!-- normal -->
<item android:state_focused="true"
android:drawable="@drawable/loading_button_help" /> <!-- normal disable focused -->
<item
android:drawable="@drawable/loading_button_help" /> <!-- normal disable -->
</selector>
而loading_icon_help
只是一张PNG图片。
答案 0 :(得分:3)
在loading_button_help.xml中你使用@ drawable / loading_button_help?我认为你有循环参考。尝试重命名您的xml或图像文件。