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

时间:2019-11-08 08:49:21

标签: java android listview android-recyclerview android-listview

我是一名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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/simpleListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        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) 

2 个答案:

答案 0 :(得分:1)

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

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

例如android:padding="5dp"

从padding属性中删除@mipmap/ic_launcher,然后如上所述进行添加。

答案 1 :(得分:0)

在您的activity_listview中,像下面一样更改padding的{​​{1}}值

TextView