我尝试按照slidenerds youtube 6 part tutorial创建包含自定义行的列表视图。在他的教程中,他使用了1张图片和2张textview,我需要3张图片和3张textview,当我运行应用程序时,它崩溃了,试图加载列表视图。
------------- homesinglerow.xml -----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lilly"
android:textColor="#2c7134"
android:textStyle="bold"
android:id="@+id/dogName"
android:textSize="24dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/dogimage"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:clickable="true"
android:onClick="sendProfile"
android:scaleType="fitXY"
android:src="@drawable/lilly"
android:background="?attr/selectableItemBackground" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timestamp 1"
android:textColor="#2c7134"
android:id="@+id/timestamp1"/>
<ImageButton
android:id="@+id/dog1num1"
android:layout_width="75dip"
android:layout_height="75dip"
android:layout_marginLeft="10dp"
android:clickable="true"
android:onClick="NumberOne"
android:scaleType="fitXY"
android:src="@drawable/num1"
android:background="?attr/selectableItemBackground" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timestamp 1"
android:textColor="#2c7134"
android:id="@+id/timestamp2"/>
<ImageButton
android:id="@+id/dog1num2"
android:layout_width="75dip"
android:layout_height="75dip"
android:layout_marginLeft="10dp"
android:clickable="true"
android:onClick="NumberOne"
android:scaleType="fitXY"
android:src="@drawable/num2"
android:background="?attr/selectableItemBackground"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
------------------ Homelistview.xml -----------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="cs495capstone.edu.bsu.doggydid.HomeActivityListview
"
android:background="@drawable/screenbackground">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/selectprofiletext"
android:textColor="#2c7134"
android:text="Select a profile or make a post:"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:entries="@array/dogs"
android:layout_below="@id/selectprofiletext"
android:layout_above="@+id/newdogbutton" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add new dog"
android:id="@+id/newdogbutton"
android:background="#2c7134"
android:allowUndo="true"
android:textColor="#f3ca83"
android:clickable="true"
android:onClick="AddNewDog"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
---------------- Homeactivitylistview.java -----------------------
public class HomeActivityListview extends AppCompatActivity {
ListView list;
Context context;
ArrayList dogName;
ArrayList dogID;
int [] dogImages = {R.drawable.lilly, R.drawable.dogpic};
public static int [] dogNames={R.id.dogName, R.id.dogName};
int [] num1={R.id.dog1num1, R.id.dog1num1, R.id.dog1num1, R.id.dog1num1, R.id.dog1num1};
int [] num2={R.id.dog1num2, R.id.dog1num2, R.id.dog1num2, R.id.dog1num2, R.id.dog1num2};
String[] names;
String[] timestamp1;
String[] timestamp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity_listview);
Resources res = getResources();
names=res.getStringArray(R.array.names);
timestamp1=res.getStringArray(R.array.timestamps1);
timestamp2=res.getStringArray(R.array.timestamps2);
context=this;
list =(ListView) findViewById(R.id.listView);
dogAdapter adapter=new dogAdapter(this, names, dogImages, num1, num2, timestamp1, timestamp2);
list.setAdapter(adapter);
}
class dogAdapter extends ArrayAdapter<String>
{
Context context;
int[] dogImages;
int[] num1;
int[] num2;
String[] nameArray;
String[] timestamp1Array;
String[] timestamp2Array;
dogAdapter(Context c, String[] names, int imgs[], int numb1[], int numb2[], String[] time1, String[] time2)
{
super(c,R.layout.home_single_row,R.id.dogName,names);
this.context=c;
this.dogImages=imgs;
this.num1=numb1;
this.num2=numb2;
this.nameArray=names;
this.timestamp1Array=time1;
this.timestamp2Array=time2;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.home_single_row,parent, false);
ImageView dogImg=(ImageView) row.findViewById(R.id.dogimage);
ImageView img1=(ImageView) row.findViewById(R.id.dog1num1);
ImageView img2=(ImageView) row.findViewById(R.id.dog1num2);
TextView name=(TextView) row.findViewById(R.id.dogName);
TextView time1=(TextView) row.findViewById(R.id.timestamp1);
TextView time2=(TextView) row.findViewById(R.id.timestamp2);
dogImg.setImageResource(dogImages[position]);
img1.setImageResource(dogImages[position]);
img2.setImageResource(dogImages[position]);
name.setText(nameArray[position]);
time1.setText(timestamp1Array[position]);
time2.setText(timestamp2Array[position]);
return row;
}
}
public void sendProfile(View view) {
Intent intent = new Intent(getApplicationContext(), DogProfileActivity.class);
startActivity(intent);
}
public void NumberOne(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number one");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void NumberTwo(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number two");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void AddNewDog(View view) {
Intent intent = new Intent(getApplicationContext(), NewDogActivity.class);
startActivity(intent);
}
}
04-22 15:49:36.597 28628-28628 / cs495capstone.edu.bsu.doggydid E / AndroidRuntime:FATAL EXCEPTION:main 处理:cs495capstone.edu.bsu.doggydid,PID:28628 java.lang.OutOfMemoryError:无法分配带有738608个空闲字节的4194316字节分配和721KB直到OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) 在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) 在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228) 在android.content.res.Resources.loadDrawableForCookie(Resources.java:4211) 在android.content.res.Resources.loadDrawable(Resources.java:4085) 在android.content.res.Resources.loadDrawable(Resources.java:3935) 在android.content.res.TypedArray.getDrawable(TypedArray.java:886) 在android.widget.ImageView。(ImageView.java:157) 在android.widget.ImageButton。(ImageButton.java:85) 在android.widget.ImageButton。(ImageButton.java:81) 在android.support.v7.widget.AppCompatImageButton。(AppCompatImageButton.java:59) 在android.support.v7.widget.AppCompatImageButton。(AppCompatImageButton.java:55) 在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107) 在android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938) 在android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992) 在android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 在android.view.LayoutInflater.inflate(LayoutInflater.java:527) 在android.view.LayoutInflater.inflate(LayoutInflater.java:429) at cs495capstone.edu.bsu.doggydid.HomeActivityListview $ dogAdapter.getView(HomeActivityListview.java:74) 在android.widget.AbsListView.obtainView(AbsListView.java:2929) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1305) 在android.widget.ListView.onMeasure(ListView.java:1212) 在android.view.View.measure(View.java:20151) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.o
答案 0 :(得分:0)
你错过了:
list.setAdapter(dogAdapter);
setAdapter() 设置此ListView背后的数据。传递给此方法的适配器可能由WrapperListAdapter包装, 取决于当前使用的ListView功能。例如, 添加页眉和/或页脚将导致适配器被包装。
我认为视频中没有解释这一重点!
//Get ListView reference.
list =(ListView) findViewById(R.id.listView);
//create the Adapter instance.
dogAdapter adapter=new dogAdapter(this, names, dogImages, num1, num2, timestamp1, timestamp2);
//Set the adapter to the ListView!
list.setAdapter(dogAdapter);