试图用数组文本填充列表视图
Posted
技术标签:
【中文标题】试图用数组文本填充列表视图【英文标题】:Trying to populate listview with array text 【发布时间】:2021-04-29 23:30:21 【问题描述】:我有一个列表视图,我试图通过将行 xml 的文本更改为文件夹中的文件路径来填充它(这样我以后可以对文件执行操作。我在启动活动时遇到了崩溃。
我的代码: FileView.java
package com.loopbreakr.firstpdf;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.List;
public class FileView extends AppCompatActivity
ListView listView;
String filePath = "";
List<String> stringList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_view);
listView = findViewById(R.id.listView);
filePath = "PDF_files";
File file = new File(getExternalFilesDir(filePath).toString());
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++)
stringList.add(fileList[i].getName());
String[] modded = (String[]) stringList.toArray();
MyAdapter adapter = new MyAdapter(this, modded);
listView.setAdapter(adapter);
class MyAdapter extends ArrayAdapter<String>
Context context;
String rTitle[];
MyAdapter(Context c, String title[])
super(c, R.layout.row, R.id.activityTitle, title);
this.context = c;
this.rTitle = title;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row, parent, false);
TextView myTitle = row.findViewById(R.id.activityTitle);
myTitle.setText(rTitle[position]);
return row;
activity_file_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context=".FileView">
<ListView
android:layout_
android:layout_
android:id="@+id/listView"/>
</LinearLayout>
行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_
android:layout_
android:padding="16dp">
<ImageView
android:layout_
android:layout_
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
/>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:id="@+id/activityTitle"
android:text="Main Title"
android:textColor="#000"
style="bold"
android:layout_margin="5dp"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
我收到此错误:
2021-01-26 01:49:46.447 22787-22787/com.loopbreakr.firstpdf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.loopbreakr.firstpdf, PID: 22787
java.lang.RuntimeException: Unable to start activity ComponentInfocom.loopbreakr.firstpdf/com.loopbreakr.firstpdf.FileView: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.loopbreakr.firstpdf.FileView.onCreate(FileView.java:35)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
似乎是一个空对象引用,但我不确定如何解决
【问题讨论】:
【参考方案1】:问题的根源是stringList.add(fileList[i].getName());
,其中stringList
未初始化。所以尝试以下,
listView = findViewById(R.id.listView);
// initialization code
stringList = new ArrayList();
String[] modded = (String[]) stringList.toArray(); // comment this line out
// pass the ArrayList instead of Array
MyAdapter adapter = new MyAdapter(this, stringList);
//rest of the logic remains same
编辑:
虽然您的适配器似乎可以工作,但膨胀逻辑是错误的,因为您没有回收视图,而是仅为要显示的每个数据项创建 新视图,因此请尝试以下操作
// recommended adapter code
class MyAdapter extends ArrayAdapter<String>
Context context;
List<String> rTitle;
MyAdapter(Context c, List<String> title)
// since you will be inflating views manually, you don't need to pass any layoutId instead just pass 0
super(c, 0 , title);
this.context = c;
this.rTitle = title;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
// convertView contains the old used view if any, else create a new view
if(convertView == null)
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row, parent, false);
TextView myTitle = convertView.findViewById(R.id.activityTitle);
myTitle.setText(rTitle.get(position));
//return convertview
return convertView;
【讨论】:
感谢逻辑上的更正。虽然当我使用你的代码时我仍然遇到了崩溃:2021-01-26 13:42:38.939 24325-24325/com.loopbreakr.firstpdf E/AndroidRuntime: FATAL EXCEPTION: main Process: com.loopbreakr.firstpdf, PID: 24325 java.lang.RuntimeException:无法启动活动 ComponentInfocom.loopbreakr.firstpdf/com.loopbreakr.firstpdf.FileView:java.lang.ClassCastException:java.lang.Object[] 无法转换为 java.lang.String[ ] 现在检查答案,更改的要点 - 直接将 List你需要初始化你的数组列表
List<String> stringList = new ArrayList<>();
【讨论】:
以上是关于试图用数组文本填充列表视图的主要内容,如果未能解决你的问题,请参考以下文章