为什么这个简单的Android应用程序在使用ListView后会崩溃?

时间:2016-04-17 17:15:12

标签: android listview

我写了一个Android应用程序来理解ListView的概念。

MainActivity.java文件是:

package com.example.swasik.firstday;

import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    String Age[] = {"11","222","33","44444","551"};
    String City[] = {"Pune","Delhi","Chennai","Kolkata"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        AutoCompleteTextView a = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,Age);
        a.setAdapter(adapter);

        ListView b = (ListView) findViewById(R.id.listView);
        ArrayAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, City);
        b.setAdapter(listAdapter);

    }
}

activity_main.xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.swasik.firstday.MainActivity">

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView"
        android:completionThreshold="1"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        >

    </ListView>


</RelativeLayout>

AndroidManifest.xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.swasik.firstday">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在移动设备上运行应用程序时会显示以下消息:“不幸的是App已停止”如何解决此问题?

logcat文件显示:

04-17 22:50:21.794 1576-1576/com.example.swasik.firstday E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                                                                               at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                                                                               at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
                                                                               at android.widget.AbsListView.obtainView(AbsListView.java:2465)
                                                                               at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
                                                                               at android.widget.ListView.onMeasure(ListView.java:1162)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
                                                                               at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                               at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
                                                                               at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
                                                                               at android.view.View.measure(View.java:15598)
                                                                               at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2006)
                                                                               at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1245)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1420)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1138)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4645)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:525)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                                                                               at android.os.Handler.handleCallback(Handler.java:615)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                               at android.os.Looper.loop(Looper.java:174)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4952)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                                                                               at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
                                                                               at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
                                                                               at android.widget.AbsListView.obtainView(AbsListView.java:2465) 
                                                                               at android.widget.ListView.measureHeightOfChildren(ListView.java:1250) 
                                                                               at android.widget.ListView.onMeasure(ListView.java:1162) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602) 
                                                                               at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118) 
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
                                                                               at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118) 
                                                                               at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118) 
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118) 
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396) 
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118) 
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423) 
                                                                               at android.view.View.measure(View.java:15598) 
                                                                               at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2006) 
                                                                               at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1245) 
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1420) 
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1138) 
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4645) 
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:525) 
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
                                                                               at android.os.Handler.handleCallback(Handler.java:615) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                               at android.os.Looper.loop(Looper.java:174) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4952) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 
04-17 22:50:30.614 1576-1576/com.example.swasik.firstday I/Process: Sending signal. PID: 1576 SIG: 9

3 个答案:

答案 0 :(得分:1)

ArrayAdapter期望您提供的布局资源包含TextView作为其根元素,否则它不知道如何将数据绑定到视图。如果要指定更复杂的布局,请使用在该布局中同时获取布局资源和TextView的id的构造函数:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
        R.layout.activity_list_item, R.id.some_textview, City);

继续前进,您可能会发现您的数据变得更加复杂,并且ArrayAdapter根本不足以完成您想要的任务。此时,您应该考虑编写自己的ListAdapter类。我会推荐你​​一个名为The World Of ListView的旧视频,它涵盖了LIstViews的基础知识。 (在某些时候你应该考虑使用RecyclerView而不是ListView)。

答案 1 :(得分:0)

ArrayAdapter&lt;&gt;您正在使用的构造函数不提供它需要的TextView。请尝试使用以下构造函数。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, Age);

答案 2 :(得分:0)

使用它可以解决您的问题:

ArrayAdapter listAdapter = new ArrayAdapter<String (this,android.R.layout.simple_list_item_1, City);