我收到以下错误,主要是在Android 4.1和4.4上:
java.lang.RuntimeException: Unable to create service se.weblink.weblinkunified.services.FloatingWindowService: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2406)
at android.app.ActivityThread.access$1700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at se.weblink.weblinkunified.services.FloatingWindowService.addViewToWindow(FloatingWindowService.java:122)
at se.weblink.weblinkunified.services.FloatingWindowService.onCreate(FloatingWindowService.java:92)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2396)
... 10 more
addViewToWindow函数:
final LayoutInflater layoutInflater = LayoutInflater.from(this);
popupWindow = layoutInflater.inflate(R.layout.window_floating_call, null);
final WindowManager.LayoutParams params = setupLayoutParams();
final View.OnClickListener clickListener = getViewClickListener();
final View.OnTouchListener touchListener = getTouchListener(params);
popupWindow.setOnClickListener(clickListener);
popupWindow.setOnTouchListener(touchListener);
final View overlayView = popupWindow.findViewById(R.id.overlay_view);
overlayView.setOnClickListener(clickListener);
overlayView.setOnTouchListener(touchListener);
final View topContainer = popupWindow.findViewById(R.id.call_top_container);
buttonContainer = (LinearLayout) popupWindow.findViewById(R.id.call_bottom_container);
buttonContainer.addView(getActiveCallButtons());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final Resources resources = getResources();
topContainer.setBackgroundColor(sharedPreferences.getInt(Constants.THEME_COLOR_PRIMARY, resources.getColor(R.color.accent_color_default)));
setupCloseButton();
windowManager.addView(popupWindow, params);
popupWindow.setTranslationY(Tools.getDensityIndependentSize(-120, resources));
popupWindow.setVisibility(View.GONE);
xml文件&#34; window_floating_call&#34;:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:id="@+id/call_top_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/accent_color_default"
>
<ImageView
android:id="@+id/contact_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_contact_picture"
/>
<TextView
android:id="@+id/contact_initial_label"
android:layout_width="55dp"
android:layout_height="55dp"
android:gravity="center"
android:layout_marginRight="10dp"
android:textSize="25dp"
android:fontFamily="sans-serif-light"/>
<TextView
android:id="@+id/incoming_call_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/contact_image"
android:textColor="@color/text_color_bright"
android:text="@string/incoming_call"
android:textSize="10sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="@+id/contact_name_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/incoming_call_label"
android:layout_marginTop="4dp"
android:layout_toRightOf="@+id/contact_image"
android:textColor="@color/text_color_bright"
android:textSize="18sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="@+id/contact_number_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/contact_image"
android:gravity="top"
android:layout_below="@+id/contact_name_label"
android:fontFamily="sans-serif-light"
android:textSize="14sp"
android:textColor="@color/text_color_bright"/>
<View
android:id="@+id/overlay_view"
android:layout_width="match_parent"
android:layout_height="54dp"/>
<ImageButton
android:id="@+id/close_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_cancel"
android:alpha="0.7"
style="@style/Base.Widget.AppCompat.ActionButton"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/call_bottom_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/call_top_container"
android:orientation="vertical"
android:weightSum="1"
>
</LinearLayout>
我无法弄清楚导致错误的原因。 Android 5.1上不会出现此问题。而且我似乎无法理解为什么这段代码无法在Android 4.1,4.2,4.4上运行。
如果有人能帮助我,我感激不尽。提前谢谢!
忘记包含android.content.res.Resources $ NotFoundException错误。但是例外说资源文件不是一个可绘制的。除了主要的drawable文件夹(drawable)之外,我在所有其他可绘制文件夹(drawable-hdpi,drawable-mdpi等)中都有图像ic_action_cancel。我尝试将.png文件添加到主文件夹但它不起作用。
答案 0 :(得分:0)
我遇到了和你一样的问题。我首先使用自己写的inflateUtils
。
代码是:
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
getContext()
方法返回applicationContext
。它出现了InflateExcepition
。
现在我改为使用View.inflate(context, resId, null)
,上下文是我的活动。它现在很有趣!希望能帮到你。