我开始学习Android开发课程,同时我正在学习自定义适配器 执行此代码后,应用程序崩溃
这是我的全部代码
customlistview.java
package com.example.vamsi.customadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by vamsi on 3/12/2017.
*/
public class customlistviewadadapter extends BaseAdapter {
private Context mcontext;
private ArrayList<HashMap<String,String>> books;
private static LayoutInflater inflater=null;
public customlistviewadadapter(Context context, ArrayList<HashMap<String,String>> data){
mcontext=context;
books=data;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return books.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =convertView;
if(convertView==null){
view=inflater.inflate(R.layout.list_row,null);
TextView title = (TextView)view.findViewById(R.id.title);
TextView author=(TextView)view.findViewById(R.id.author);
TextView pages=(TextView)view.findViewById(R.id.pages);
ImageView image=(ImageView)view.findViewById(R.id.listimg);
HashMap<String,String> mbook=new HashMap<>();
mbook = books.get(position);
title.setText(mbook.get("title"));
author.setText(mbook.get("author"));
pages.setText(mbook.get("pages"));
}
return view;
}
}
MainActivity.java
package com.example.vamsi.customadapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listview;
private customlistviewadadapter custlistadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] bookTitles=new String[]{
"alchemist",
"gulivers trvvels",
"brief history of time",
"get away with murder",
"god thank you",
"books books books"
};
final String[] pages=new String[]{
"100 pages",
"120 pages",
"111 pages",
"121 pages",
"123 pages"
};
final String[] authors=new String[]{
"lol",
"donno",
"stephen",
"boom",
"god",
"books again"
};
ArrayList<HashMap<String,String>> authorlist=new ArrayList<>();
for (int i=0;i<6;i++){
HashMap<String,String> data=new HashMap<>();
data.put("title",bookTitles[i]);
data.put("pages",pages[i]);
data.put("author",authors[i]);
authorlist.add(data);
}
listview=(ListView)findViewById(R.id.listview1);
custlistadapter=new customlistviewadadapter(getApplicationContext(),authorlist);
listview.setAdapter(custlistadapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int mypos=position;
String itemclicked=listview.getItemAtPosition(mypos).toString();
Toast.makeText(getApplicationContext(),"id cliccked"+itemclicked,Toast.LENGTH_SHORT).show();
}
});
}
}
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/listselector"
android:orientation="horizontal"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/thumbnail"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:background="#ffeeeade"
android:layout_marginRight="5dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/listimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_alignTop="@+id/thumbnail"
android:layout_width="wrap_content"
android:text="the alchemist"
android:textSize="14sp"
android:textColor="#040404"
android:layout_toRightOf="@+id/thumbnail"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/author"
android:layout_below="@+id/title"
android:text="paulo"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#ffde3a3d"
android:layout_marginTop="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="234 pages"
android:id="@+id/pages"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/title"
android:layout_marginRight="10dp"
android:textColor="#10bcc9"
android:gravity="right"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
activity_main.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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vamsi.customadapter.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/listview1"
android:listSelector="@drawable/listselector"
android:divider="@color/colorAccent"
android:dividerHeight="1dp" />
</RelativeLayout>
我尝试启用调试,这是控制台输出。
V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@28f08ea
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a9a47db
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vamsi.customadapter, PID: 6371
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vamsi.customadapter/com.example.vamsi.customadapter.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.example.vamsi.customadapter.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6760)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 6371 SIG: 9
Disconnected from the target VM, address: 'localhost:8612', transport: 'socket'
我正在使用API级别25 任何帮助将不胜感激
答案 0 :(得分:1)
pages
有5个值,而不是其他6个值。
除此之外,您还对数组的长度进行了硬编码。
for (int i=0; i < 6; i++){ // Here
// Code removed
}
所以,不要这样做。将6
替换为5
,或者最好是bookTitles.length
...
甚至更好。了解对象
public static class BookData {
public String title, author, pages;
}
并制作ArrayList<BookData>