由于错误而无法启动基本应用程序(UnsupportedOperationException:无法转换为维度:type = 0x1)

时间:2016-04-03 17:37:59

标签: java android android-cardview android-studio-2.0

我正在学习创建CardView。我可以在布局文件的设计选项卡和布局的文本部分的预览部分中看到布局的样子。

然而,当我在模拟器或Android设备上运行应用程序时,它会抛出一个错误,上面写着“不幸的是,CardActivity已停止工作。”

我不知道我做错了什么,所以请帮忙。

这是activity_main.xml文件内容。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <!-- A CardView That contains a TextView -->
        <android.support.v7.widget.CardView 
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            card_view:cardUseCompatPadding="true"
            card_view:cardElevation="4dp"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="5dp"
            android:background="#dddddd">

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent"
                android:weightSum="1"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:background="#4f76d2">

                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:scaleType="centerCrop"
                        android:layout_margin="5dp"
                        android:src="@drawable/image" />

                    <TextView
                        android:id="@+id/info_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="?android:attr/textAppearanceLarge"
                        android:layout_margin="20dp"
                        android:textColor="#ffffff"
                        android:textIsSelectable="true" />

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/para_text"
                    android:textSize="?android:attr/textAppearanceSmall"
                    android:textColor="#88b0f5"
                    android:padding="5dp" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

这是MainActivity.java文件内容。

package com.example.android.cardactivity;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

这就是Android监视器中出现的所有内容。

04-03 22:55:13.164 20095-20095/com.example.android.cardactivity E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.android.cardactivity, PID: 20095
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cardactivity/com.example.android.cardactivity.MainActivity}: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class android.widget.TextView
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
          at android.app.ActivityThread.-wrap11(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5417)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
       Caused by: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class android.widget.TextView
          at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
          at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
          at android.app.Activity.setContentView(Activity.java:2166)
          at com.example.android.cardactivity.MainActivity.onCreate(MainActivity.java:10)
          at android.app.Activity.performCreate(Activity.java:6237)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
       Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class android.widget.TextView
          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:694)
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
          at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
          at android.app.Activity.setContentView(Activity.java:2166) 
          at com.example.android.cardactivity.MainActivity.onCreate(MainActivity.java:10) 
          at android.app.Activity.performCreate(Activity.java:6237) 
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Constructor.newInstance(Native Method)
          at android.view.LayoutInflater.createView(LayoutInflater.java:619)
          at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
          at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
          at android.app.Activity.setContentView(Activity.java:2166) 
          at com.example.android.cardactivity.MainActivity.onCreate(MainActivity.java:10) 
          at android.app.Activity.performCreate(Activity.java:6237) 
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
       Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
          at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
          at android.widget.TextView.<init>(TextView.java:1055)
          at android.widget.TextView.<init>(TextView.java:671)
          at android.widget.TextView.<init>(TextView.java:667)
          at java.lang.reflect.Constructor.newInstance(Native Method) 
          at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
          at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
          at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
          at android.app.Activity.setContentView(Activity.java:2166) 
          at com.example.android.cardactivity.MainActivity.onCreate(MainActivity.java:10) 
          at android.app.Activity.performCreate(Activity.java:6237) 
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:2)

您要为属性android:textSize指定无效值?android:attr/textAppearanceLarge。您应该将值分配给android:textAppearance,如下所示:

<TextView
    android:id="@+id/info_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_margin="20dp"
    android:textColor="#ffffff"
    android:textIsSelectable="true" />
  

java.lang.UnsupportedOperationException:无法转换为维度:   类型为0x1 =

以上是在堆栈跟踪底部找到的异常的关键部分,可以帮助您找出错误。

基本上,属性android:textSize需要一个值的维度。

?android:attr/textAppearanceLarge不是维度值,因此应用程序在尝试将其转换为一个时会崩溃。