gpt4 book ai didi

java - 图像和字符串数组的 ListView 不起作用

转载 作者:行者123 更新时间:2023-12-02 12:16:09 25 4
gpt4 key购买 nike

我是 Android 编程新手。有人可以帮我吗?我正在测试如何使用 arrayadapter 在 ListView 中显示图像和字符串。我什至以不同的方法尝试了基础适配器,但出现同样的问题,应用程序崩溃并且从未运行。

这是我的MainActivity.java的代码

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {
ListView l;
int [] Images = {R.drawable.img1,R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10};
String [] Titles;
String [] Description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources=getResources();
Titles = resources.getStringArray(R.array.Title);
Description = resources.getStringArray(R.array.Description);
l = findViewById(R.id.listView);
PopulateRows arrayAdapter = new PopulateRows(this, Titles,Images, Description);
l.setAdapter(arrayAdapter);

}
class PopulateRows extends ArrayAdapter<String>{
Context context;
int [] Images;
String[] Titles;
String [] Description;
PopulateRows(Context context, String[] Titles, int[] imgs, String[] Description){
super(context,R.layout.custom_layout, R.id.textView, Titles);
this.context=context;
this.Images=imgs;
this.Titles=Titles;
this.Description=Description;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_layout,parent,false);

ImageView imageView = row.findViewById(R.id.imageView);
TextView textView = row.findViewById(R.id.textView);
TextView textView1 = row.findViewById(R.id.textView1);

imageView.setImageResource(Images[position]);
textView.setText(Titles[position]);
textView1.setText(Description[position]);
return row;
}
}
}

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.name.mypackage.MainActivity">

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

</ListView>

</android.support.constraint.ConstraintLayout>

这里还是另一个custom_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginStart="28dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:text="TextView"
android:layout_alignTop="@+id/imageView"
android:layout_toEndOf="@+id/imageView" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_alignStart="@+id/textView"
android:text="TextView" />

</RelativeLayout>

当我运行此代码时,我总是会收到以下错误并且应用程序崩溃:

--------- beginning of crash 09-12 10:11:44.925 4706-4706/com.example.name.mypackageE/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.name.mypackage, PID: 4706 java.lang.OutOfMemoryError: Failed to allocate a 286654476 byte allocation with 4896858 free bytes and 248MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2638) at android.content.res.Resources.loadDrawable(Resources.java:2543) at android.content.res.Resources.getDrawable(Resources.java:806) at android.content.Context.getDrawable(Context.java:458) at android.widget.ImageView.resolveUri(ImageView.java:826) at android.widget.ImageView.setImageResource(ImageView.java:433) at com.example.name.mypackage.MainActivity$PopulateRows.getView(MainActivity.java:55) at android.widget.AbsListView.obtainView(AbsListView.java:2346) at android.widget.ListView.measureHeightOfChildren(ListView.java:1281) at android.widget.ListView.onMeasure(ListView.java:1188) at android.view.View.measure(View.java:18809) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) at android.view.View.measure(View.java:18809) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

使列表显示不带图像的代码,仅显示字符串数组

public class MainActivity extends Activity {
ListView l;
//int [] Images = {R.drawable.img1,R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10};
String [] Titles;
String [] Description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources=getResources();
Titles = resources.getStringArray(R.array.Title);
Description = resources.getStringArray(R.array.Description);
l = findViewById(R.id.listView);
PopulateRows arrayAdapter = new PopulateRows(this, Titles, Description);
l.setAdapter(arrayAdapter);

}
class PopulateRows extends ArrayAdapter<String>{
Context context;
//int [] Images;
String[] Titles;
String [] Description;
PopulateRows(Context context, String[] Titles, String[] Description){
super(context,R.layout.custom_layout, R.id.textView, Titles);
this.context=context;
//this.Images=imgs;
this.Titles=Titles;
this.Description=Description;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_layout,parent,false);

//ImageView imageView = row.findViewById(R.id.imageView);
TextView textView = row.findViewById(R.id.textView);
TextView textView1 = row.findViewById(R.id.textView1);

//imageView.setImageResource(Images[position]);
textView.setText(Titles[position]);
textView1.setText(Description[position]);
return row;
}
}
}

最佳答案

Java 内存不足错误意味着您的图像分辨率非常高。您的 View 未填充,这就是您收到崩溃的原因。

通过为不同的屏幕分辨率设置可绘制来调整图像分辨率可绘制--hdpi、可绘制--xhdpi、可绘制--xxhdpi、可绘制--xxxhdpi。

如果您的图像内容较多,请尝试使用 recycleview 而不是 listview。

关于java - 图像和字符串数组的 ListView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46168104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com