为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?

Posted

技术标签:

【中文标题】为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?【英文标题】:Why does my ListView crash my app instead of listing my Entries?为什么我的 ListView 使我的应用程序崩溃而不是列出我的条目? 【发布时间】:2020-03-04 21:19:44 【问题描述】:

我是一名 android 初学者,我一起复制了一些代码来获得我的第一个简单的 ListView。 但它不起作用。它没有工作,而是使我的应用程序崩溃。 非常感谢您帮助我的 ListView 正确工作,谢谢。

MainActivity.java:

package com.example.listview_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;import android.widget.ListView;

public class MainActivity extends Activity

    // Array of strings...
    ListView simpleList;
    String countryList[] = "India", "China", "australia", "Portugle", "America", "NewZealand";

    @Override   protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView)findViewById(R.id.simpleListView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.textView, countryList);
        simpleList.setAdapter(arrayAdapter);
    

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <ListView
        android:id="@+id/simpleListView"
        android:layout_
        android:layout_
        android:divider="@color/material_blue_grey_800"
        android:dividerHeight="1dp" />
</LinearLayout>

activity_listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:padding="@mipmap/ic_launcher" />

</LinearLayout>

错误日志

11-08 10:01:59.277 3676-3676/com.example.listview_test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.listview_test, PID: 3676
    android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.TextView
        at android.view.LayoutInflater.createView(LayoutInflater.java:652)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at com.android.internal.policy.impl.HwPhoneLayoutInflater.onCreateView(HwPhoneLayoutInflater.java:75)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:701)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:760)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:825)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:523)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2481)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1289)
        at android.widget.ListView.onMeasure(ListView.java:1197)
        at android.view.View.measure(View.java:17970)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654)
        at android.view.View.measure(View.java:17970)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
        at android.view.View.measure(View.java:17970)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654)
        at android.view.View.measure(View.java:17970)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2755)
        at android.view.View.measure(View.java:17970)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2438)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1418)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1642)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1296)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6742)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:826)
        at android.view.Choreographer.doCallbacks(Choreographer.java:629)
        at android.view.Choreographer.doFrame(Choreographer.java:597)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:812)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5929)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:626)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
        at com.android.internal.policy.impl.HwPhoneLayoutInflater.onCreateView(HwPhoneLayoutInflater.java:75) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:701) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:760) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:825) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:523) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:425) 
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371) 
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
        at android.widget.AbsListView.obtainView(AbsListView.java:2481) 
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1289) 
        at android.widget.ListView.onMeasure(ListView.java:1197) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465) 
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2755) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2438) 
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1418) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1642) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1296) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6742) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:826) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:629) 
        at android.view.Choreographer.doFrame(Choreographer.java:597) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:812) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5929) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) 
     Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
        at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:604)
        at android.view.View.<init>(View.java:3804)
        at android.widget.TextView.<init>(TextView.java:685)
        at android.widget.TextView.<init>(TextView.java:680)
        at android.widget.TextView.<init>(TextView.java:676)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:626) 
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
        at com.android.internal.policy.impl.HwPhoneLayoutInflater.onCreateView(HwPhoneLayoutInflater.java:75) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:701) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:760) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:825) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:523) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:425) 
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371) 
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
        at android.widget.AbsListView.obtainView(AbsListView.java:2481) 
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1289) 
        at android.widget.ListView.onMeasure(ListView.java:1197) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:785) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:654) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5710) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:465) 
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2755) 
        at android.view.View.measure(View.java:17970) 
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2438) 
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1418) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1642) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1296) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6742) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:826) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:629) 
        at android.view.Choreographer.doFrame(Choreographer.java:597) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:812) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5929) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) 

【问题讨论】:

分享错误日志.. 当你的应用程序崩溃时,你的第一个反应——是的,在 *** 上发布之前——是检查 Logcat 看看发生了什么 尝试删除android:padding="@mipmap/ic_launcher"。如果你想使用padding,那么你可以试试android:padding="8dp" 删除 android:padding="@mipmap/ic_launcher" 使 ListView 工作。它几乎没有格式化,但它是一个 ListView。非常感谢.... 行 android:padding="@mipmap/ic_launcher" 导致崩溃,用一些数值更新填充。会解决问题的 【参考方案1】:

android:padding="@mipmap/ic_launcher" 您的 activity_listView.xml 中的这一行导致错误。

`android:padding="any numeric value dp"`    

android:padding="5dp"

从 padding 属性中删除 @mipmap/ic_launcher 并按上述方法添加。

【讨论】:

谁能告诉我,我可以在错误日志的哪个位置看到崩溃的错误是什么? 二进制 XML 文件第 7 行:错误膨胀类 android.widget.TextView。您的错误表明您在第 1 行的 XML 文件中存在问题。 7关于定义TextView。由于您在TextView中输入了错误的属性,这就是它在TextView中显示错误的原因。只需阅读错误日志的前两行,他们提到了错误的原因和位置。 另外,尝试了解所有视图的所有重要属性及其值。每个视图都有一些常用的属性。有很多教程可以让你理解它们。【参考方案2】:

在您的activity_listview 中更改您的paddingTextView 值,如下所示

<TextView
   android:id="@+id/textView"
   android:layout_
   android:layout_
   android:layout_gravity="center"
  <!-- modify below line like below -->
   android:padding="5dp" 
/>

【讨论】:

以上是关于为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?的主要内容,如果未能解决你的问题,请参考以下文章

为啥意图使我的应用程序崩溃?

释放自动释放对象不会使我的应用程序崩溃,为啥?

为啥 authorizeview roles="admin" 使我的应用程序崩溃?

为啥这个旧的依赖会使我的 gradle 崩溃?

为啥大型本地数组会使我的程序崩溃,而全局数组却不会? [复制]

为啥 ntdll.dll 会使我的 c++ 可执行文件崩溃?