我正在尝试运行一个已经编写的代码,应该没问题。但是我得到了错误。
我也在寻找类似的问题,但是每个问题及其解决方案都是特定的,并且不提供可用于我的问题的信息。 换句话说,Android Studio提供的异常消息适用于无尽的可能问题。
引起问题的xml文件-activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@string/contactsTtl"
android:textSize="@dimen/TtlTxtSize"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/lvContacts"
/>
</LinearLayout>
在logcat中,我发现异常是由
引起的java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x4
以下是异常的日志记录:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jsonreading, PID: 28802
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonreading/com.example.jsonreading.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Cant convert value at index 2 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:737)
at android.widget.TextView.<init>(TextView.java:1451)
at android.widget.TextView.<init>(TextView.java:1038)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.jsonreading.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
MainActivity.java文件(不是整个文件,而是运行和获取异常所需的文件):
public class MainActivity extends AppCompatActivity {
Context context;
List<Contact> contactList;
ListView lvContacts;
ContactsAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
答案 0 :(得分:0)
此错误可能是由于未在dimens.xml
文件夹下的主values
中定义dimens变量的值,而在其他特定于版本的values
中对其进行了定义。确保已在主dimens.xml
中定义了它。还要确保您也已将其声明为适当的值。
答案 1 :(得分:0)
如果您查看所发布的日志猫过去的错误,则将看到以下行:
Caused by: java.lang.UnsupportedOperationException: Cant convert value at index 2 to dimension: type=0x4
这是真正的错误。您首先看到的错误来自一系列错误。换句话说,发生此错误,然后通货膨胀失败。
如果我错误地定义了尺寸,请说8
而不是8dp
:
<dimen name="test_dimen">8</dimen>
然后我会收到此错误:
原因:android.view.InflateException:二进制XML文件第13行:无法将索引7的值转换为维度:类型= 0x4
这与您看到的非常相似。确保尺寸有效:8dp
,8sp
,8px
等。有关详细信息,请参见documentation。
答案 2 :(得分:0)
在 build.gradle(:app) 中更新 androidx.constraintlayout:constraintlayout