我不明白为什么这段代码会导致“不幸的是,android3已经停止”
我尝试了很多在网上找到的解决方案,但似乎都不起作用,我不知道原因,请有人帮助我。
这是我的activity_main.xml 文件
activity_main.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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/theListView">
</ListView>
</LinearLayout>
这是我的 MainActivity.java 文件
MainActivity.java:
package com.snoott.android3;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String names[] = {"Dustin Moskovitz" , "Elon Musk" , "Bill Gates" , "Mark Zuckerberg" , "Larry Page" ,
"Sergey Brin" , "Steve Jobs" , "Tim Cook"};
final Integer[] images = {R.drawable.dustin , R.drawable.elon , R.drawable.gate , R.drawable.mark , R.drawable.page ,
R.drawable.sergey , R.drawable.steve , R.drawable.tim};
final String descriptions [] = {"Co-founder of Facebook" , "CEO Tesla" , "Founder Microsoft" , "Co-founder , CEO of Facebook" ,
"Chairman Alphabet Inc." , "President Alphabet Inc." , "Founder Apple" , "CEO Apple"};
ArrayList<Profile> profiles = new ArrayList<Profile>();
for(int i = 0; i < descriptions.length; ++i){
profiles.add(new Profile(names[i] , images[i] , descriptions[i]));
}
ArrayAdapter<Profile> listAdapter = new CustomAdapter(this , profiles);
listView = (ListView)findViewById(R.id.theListView);
listView.setAdapter(listAdapter);
}
}
这是我的 Profile.java 文件
Profile.java:
package com.snoott.android3;
public class Profile {
public String name;
public Integer profileImage;
public String description;
public Profile(){}
public Profile(String name , Integer profile , String description){
this.name = name;
this.profileImage = profile;
this.description = description;
}
}
这是我的 CustomAdapter 文件:
CustomAdapter.java:
package com.snoott.android3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<Profile>{
public CustomAdapter(@NonNull Context context, ArrayList<Profile> resource) {
super(context, R.layout.custom_listview , resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
Profile currentProfile = (Profile)getItem(position);
if(convertView == null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_listview , parent , false);
viewHolder.nameTextView = (TextView)convertView.findViewById(R.id.profileName);
viewHolder.profileImage = (ImageView)convertView.findViewById(R.id.profile_image);
viewHolder.descriptionTextView = (TextView)convertView.findViewById(R.id.profileDescription);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.nameTextView.setText(currentProfile.name);
viewHolder.profileImage.setImageResource(currentProfile.profileImage);
viewHolder.descriptionTextView.setText(currentProfile.description);
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView descriptionTextView;
ImageView profileImage;
}
}
custom_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/mark"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_marginRight="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="-20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mark Zuckerberg"
android:textFontWeight="bold"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:id="@+id/profileName"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CEO of Facebook"
android:id="@+id/profileDescription"/>
</LinearLayout>
</LinearLayout>
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.snoott.android3"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
最佳答案
它必须抛出ArrayIndexOutOfBoundsException
,因为数组names
和images
长度为8但description
数组长度为 9。只需保持所有数组项大小相等即可。
关于java - Android自定义ArrayAdapter : "unfortunately, app has stopped" while scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52083963/