我是Android编程新手。有人可以帮我吗?我正在测试如何使用arrayadapter在列表视图中显示图像和字符串。我甚至用不同的方法尝试了基本适配器,但同样的问题,应用程序崩溃,永远不会运行。
以下是 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>
当我运行此代码时,我总是会收到以下错误并且应用程序崩溃:
---------崩溃开始09-12 10:11:44.925 4706-4706 / com.example.name.mypackageE / AndroidRuntime:FATAL 例外:主要 处理:com.example.name.mypackage,PID:4706 java.lang.OutOfMemoryError:无法分配286654476字节 分配4896858个空闲字节和248MB直到OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) 在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) 在 android.content.res.Resources.loadDrawableForCookie(Resources.java:2638) 在android.content.res.Resources.loadDrawable(Resources.java:2543) 在android.content.res.Resources.getDrawable(Resources.java:806) 在android.content.Context.getDrawable(Context.java:458) 在android.widget.ImageView.resolveUri(ImageView.java:826) 在android.widget.ImageView.setImageResource(ImageView.java:433) 在 com.example.name.mypackage.MainActivity $ PopulateRows.getView(MainActivity.java:55) 在android.widget.AbsListView.obtainView(AbsListView.java:2346) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1281) 在android.widget.ListView.onMeasure(ListView.java:1188) 在android.view.View.measure(View.java:18809) 在 android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) 在 android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) 在android.view.View.measure(View.java:18809) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:18809) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18809) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在 com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643) 在android.view.View.measure(View.java:18809) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6060) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:746) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在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;
}
}
}
答案 0 :(得分:1)
Java内存不足错误意味着图像的分辨率非常高。 您的观点不会被填充,这就是您收到崩溃的原因。
通过绘制不同的屏幕分辨率来调整图像分辨率 drawable - hdpi,drawable - xhdpi,drawable - xxhdpi,drawable - xxxhdpi。
如果您的图片内容更多,请尝试使用recycleview,而不是listview。
答案 1 :(得分:0)
尝试使用具有最小尺寸的图像,因为您正在使用的图像会导致代码崩溃,您可以使用更多
Hello world
在android清单中如下
android:hardwareAccelerated="false"
android:largeHeap="true"
通过this link获取更多信息....