我对Android开发相当新,当我试图给xml充气时,我的应用程序在启动时崩溃,出现以下错误。任何帮助表示赞赏。谢谢
日志中的异常:
01-16 17:13:34.033 1836-1836/com.example.android.inventoryapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.inventoryapp, PID: 1836
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.android.inventoryapp.ItemCursorAdapter.newView(ItemCursorAdapter.java:29)
at android.widget.CursorAdapter.getView(CursorAdapter.java:285)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1716)
at android.widget.AbsListView.onLayout(AbsListView.java:2162)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
01-16 17:13:34.034 1836-1836/com.example.android.inventoryapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 14 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:720)
at android.view.View.<init>(View.java:4205)
at android.view.ViewGroup.<init>(ViewGroup.java:579)
at android.widget.LinearLayout.<init>(LinearLayout.java:211)
at android.widget.LinearLayout.<init>(LinearLayout.java:207)
at android.widget.LinearLayout.<init>(LinearLayout.java:203)
... 58 more
我的xml用于在另一个xml的列表视图中使用的列表项:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:padding="@dimen/activity_margin">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
tools:src="@mipmap/ic_launcher_round"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#2B3D4D"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/list_item_margin"
tools:text ="Sunglasses"
android:layout_marginStart="@dimen/list_item_margin" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#AEB6BD"
android:layout_marginLeft="@dimen/list_item_margin"
android:textStyle="bold"
android:layout_gravity="center"
tools:text ="7"
android:layout_marginStart="@dimen/list_item_margin" />
<Button
android:id="@+id/sell_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="@dimen/list_item_margin"
tools:text="Sell"
android:layout_marginStart="@dimen/list_item_margin" />
</LinearLayout>
梦诗:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_margin">16</dimen>
<dimen name="list_item_margin">35dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>
Stackoverflow要求我添加更多细节,但据我所知,没有任何细节,所以我正在编写这些行来传递错误。
答案 0 :(得分:3)
您的dimens.xml
文件是否在values
文件夹下?如果没有,那么这可能会导致问题。如果您为某些API提供了维度,但未在values/dimens.xml
下提供所有API,那么膨胀应该无效。
编辑: activity_main
需要dp
附加含义:<dimen name="activity_margin">16dp</dimen>
答案 1 :(得分:1)
你忘了在这里添加DP:
<dimen name="activity_margin">16</dimen>
应该是:
<dimen name="activity_margin">16dp</dimen>