【问题标题】:How to avoid crashing for an listview android app [closed]如何避免listview android应用程序崩溃[关闭]
【发布时间】:2021-01-28 21:28:23
【问题描述】:

我在教程的帮助下构建了一个 android 应用程序。

这是 MainActivity.java:-

package com.example.listdisplay;

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...
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
            "WebOS","Ubuntu","Windows7","Max OS X", "PHP", "Java", "HTML", "CSS", "JavaScript","MySQL"};

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

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

这是activity_main.xml:-

<LinearLayout 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:orientation="vertical">

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

</LinearLayout>

这是activity_listview.xml:-

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:orientation="horizontal">
</TextView>

当我将 textview 更改为任何其他视图时,例如 cardview 等等。该应用程序将崩溃。 我想用卡片视图和布局做更多的样式。就像我也尝试过使用线性布局一样。我在线性布局中插入了这个带有按钮的文本视图。但是,它仍然会崩溃。

请帮帮我。

【问题讨论】:

标签: android android-layout android-listview android-arrayadapter


【解决方案1】:

您不能从 activity_listview.xml 更改 TextView,因为 ArrayAdapterconstructor 需要一个只有 EditText 的布局。

如果你想修改它,你必须实现一个自定义的ArrayAdapter创建一个继承自它的类。

您可以按照下一个 Medium 教程来实现这一点。 https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

如果要添加Button 或其他内容,则需要自定义ArrayAdapter

【讨论】:

    【解决方案2】:

    如果您想使用卡片视图进行更多样式设置,因此您不需要将文本视图更改为卡片视图,只需编写您的代码,如下面的代码...

    <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardCornerRadius="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp">
                <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/label"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:padding="5dip"
                 android:textSize="16dip"
                 android:textStyle="bold"
                 android:orientation="horizontal">
                 </TextView>
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    

    【讨论】:

    • 还是会崩溃。
    【解决方案3】:

    创建您的自定义 xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="8dp">
    
            <TextView
                android:id="@+id/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="This is just text" />
        </LinearLayout>
    
    </androidx.cardview.widget.CardView>
    

    重要部分是android:id="@+id/label" in TextView

    并将R.id.label 添加到ArrayAdapter 的第三个参数中,如下所示:

    ArrayAdapter adapter = new ArrayAdapter<String>(this,
                    R.layout.activity_listview, R.id.label, mobileArray);
    

    【讨论】:

    • 还是会崩溃。
    • 尝试不使用 CardView,如果它的工作你需要 androidx 依赖来使用 CardView。
    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多