Android 在listview未捕获处理程序之后传递到另一个活动时出错:由于未捕获异常而退出线程main
我有个问题Android 在listview未捕获处理程序之后传递到另一个活动时出错:由于未捕获异常而退出线程main,android,sqlite,listview,arraylist,adapter,Android,Sqlite,Listview,Arraylist,Adapter,我有个问题 02-20 02:57:09.854: E/AndroidRuntime(12431): Uncaught handler: thread main exiting due to uncaught exception" ……以后 02-20 02:57:09.864: E/AndroidRuntime(12431): java.lang.ClassCastException: java.util.ArrayList …在使用SQLite填充的ListView中,ListView工
02-20 02:57:09.854: E/AndroidRuntime(12431): Uncaught handler: thread main exiting due to uncaught exception"
……以后
02-20 02:57:09.864: E/AndroidRuntime(12431): java.lang.ClassCastException: java.util.ArrayList
…在使用SQLite填充的ListView中,ListView工作正常,但稍后当我尝试使用onListItemClick来获取列表视图上项目的参数并将其显示在其他布局中时,程序停止,DDMS说“未捕获处理程序:线程主线程因未捕获异常而退出”ListView中的“java.lang.ClassCastException:java.util.ArrayList”可能我处理了ArrayList的适配器吗
这是我的主要课程:
public class lay_main extends ListActivity
{
public ListView list;
public DBHelper myAdap;
protected SQLiteDatabase db;
public Cursor cursor;
DBHelper Context;
DBHelper ArrayList;
//adapter cAdapter class
protected ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay_main);
collectXML();
setupDataBase();
setupAdapter();
}
private void collectXML()
{
list = (ListView)findViewById(android.R.id.list);
}
public void setupDataBase() {
myAdap = new DBHelper(getApplicationContext());
myAdap.insertCourses();
}
public void setupAdapter()
{
if(myAdap.getCourses()!=null)
{
cAdapter adapter = new cAdapter(this, R.layout.list_courses, myAdap.getCourses());
list.setAdapter(adapter);
}
}
public void onListItemClick(ListView parent, View view, int position, long id)
{
super.onListItemClick(parent, view, position, id);
Intent intent = new Intent(this, CourseDetails.class);
Cursor cursor = (Cursor) myAdap.getItem(position);
intent.putExtra("COURSE_ID", cursor.getInt(cursor.getColumnIndex("title")));
startActivity(intent);
}
}
我的课程详细介绍了如何接收和显示数据:
public class CourseDetails<BDHelper> extends Activity {
protected TextView tTitle;
protected TextView tInstructor;
protected TextView tLength;
protected TextView tRating;
protected TextView tTopic;
protected TextView tSubject;
protected TextView tDescription;
protected int courseId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.course_details);
courseId = getIntent().getIntExtra("COURSE_ID", 0);
SQLiteDatabase db = (new DBHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT title, instructor, length, rating, topic, subject, description FROM courses",
new String[]{"" + courseId});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
tTitle = (TextView) findViewById(R.id.tTitle);
tTitle.setText(cursor.getString(cursor.getColumnIndex("title")));
tInstructor = (TextView) findViewById(R.id.tInstructor);
tInstructor.setText(cursor.getString(cursor.getColumnIndex("instructor")));
tLength = (TextView) findViewById(R.id.tLength);
tLength.setText(cursor.getString(cursor.getColumnIndex("length")));
tRating = (TextView) findViewById(R.id.tRating);
tRating.setText(cursor.getString(cursor.getColumnIndex("rating")));
tTopic = (TextView) findViewById(R.id.tTopic);
tTopic.setText(cursor.getString(cursor.getColumnIndex("topic")));
tSubject = (TextView) findViewById(R.id.tSubject);
tSubject.setText(cursor.getString(cursor.getColumnIndex("subject")));
tDescription = (TextView) findViewById(R.id.tDescription);
tDescription.setText(cursor.getString(cursor.getColumnIndex("description")));
}
}}
公共课程详细信息扩展活动{
受保护的文本视图;
受保护的文本视图结构器;
受保护的文本视图长度;
受保护的文本浏览;
受保护的文本视图;
受保护的文本视图对象;
受保护的文本视图tDescription;
受保护的int courseId;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(右布局、课程详情);
courseId=getIntent().getIntExtra(“课程ID”,0);
SQLiteDatabase db=(新的DBHelper(this)).getWritableDatabase();
Cursor=db.rawQuery(“从课程中选择标题、讲师、长度、评分、主题、主题、描述”,
新字符串[]{“+courseId});
if(cursor.getCount()==1)
{
cursor.moveToFirst();
tTitle=(TextView)findViewById(R.id.tTitle);
tTitle.setText(cursor.getString(cursor.getColumnIndex(“title”));
tInstructor=(TextView)findViewById(R.id.tInstructor);
tInstructor.setText(cursor.getString(cursor.getColumnIndex(“讲师”)));
tLength=(TextView)findViewById(R.id.tLength);
setText(cursor.getString(cursor.getColumnIndex(“长度”));
tRating=(TextView)findViewById(R.id.tRating);
grating.setText(cursor.getString(cursor.getColumnIndex(“评级”)));
tTopic=(TextView)findViewById(R.id.tTopic);
tTopic.setText(cursor.getString(cursor.getColumnIndex(“主题”));
tsObject=(TextView)findViewById(R.id.tsObject);
setText(cursor.getString(cursor.getColumnIndex(“主题”));
tDescription=(TextView)findViewById(R.id.tDescription);
tDescription.setText(cursor.getString(cursor.getColumnIndex(“description”));
}
}}
我的适配器类:
public class cAdapter extends ArrayAdapter {
Context context;
ArrayList<Courses> courses;
@SuppressWarnings("unchecked")
public cAdapter(Context context, int layout_id, ArrayList<Courses> Courses) {
super(context, layout_id, Courses);
this.context=context;
this.courses=Courses;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_courses, null);
try{
//components
TextView txt_title=(TextView)row.findViewById(R.id.txt_title);
TextView txt_instructor=(TextView)row.findViewById(R.id.txt_instructor);
TextView txt_length=(TextView)row.findViewById(R.id.txt_length);
TextView txt_rating=(TextView)row.findViewById(R.id.txt_rating);
TextView txt_topic=(TextView)row.findViewById(R.id.txt_topic);
//asign values
txt_title.setText(courses.get(position).title);
txt_instructor.setText(courses.get(position).instructor);
txt_length.setText(courses.get(position).length);
txt_rating.setText(courses.get(position).rating);
txt_topic.setText(courses.get(position).topic);
}catch(Exception ex){}
return row;
}
}
public-class-cAdapter扩展了ArrayAdapter{
语境;
ArrayList课程;
@抑制警告(“未选中”)
公共地籍簿(上下文、内部布局、阵列列表){
超级(背景、布局、课程);
this.context=context;
这个。课程=课程;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图行=充气机。充气(R.layout.list\u课程,空);
试一试{
//组成部分
TextView txt_title=(TextView)row.findViewById(R.id.txt_title);
TextView txt\u讲师=(TextView)row.findViewById(R.id.txt\u讲师);
TextView txt_长度=(TextView)row.findViewById(R.id.txt_长度);
TextView txt_评级=(TextView)row.findViewById(R.id.txt_评级);
TextView txt_主题=(TextView)row.findViewById(R.id.txt_主题);
//asign值
txt_title.setText(courses.get(position.title));
txt_讲师.setText(courses.get(position.讲师));
txt_length.setText(courses.get(position.length));
txt_rating.setText(courses.get(position.rating));
txt_topic.setText(courses.get(position.topic));
}捕获(例外情况除外){}
返回行;
}
}
并不是说我做错了,我真的很感激您的帮助。重写适配器的getItem方法并从getItem返回Cources对象
Override
public Courses getItem(int position)
{
return courses.get(positon);
}
在SetupAdapter方法中:
public void setupAdapter()
{
if(myAdap.getCourses()!=null)
{
adapter = new cAdapter(this, R.layout.list_courses, myAdap.getCourses());
list.setAdapter(adapter);
}
}
并按如下方式更改项目:
public void onListItemClick(ListView parent, View view, int position, long id)
{
super.onListItemClick(parent, view, position, id);
Intent intent = new Intent(this, CourseDetails.class);
Courses course= (Courses) adapter.getItem(position);
intent.putExtra("COURSE_ID", course.title));
startActivity(intent);
}
粘贴你的getItem方法代码。嗨,jitendra,谢谢阅读,我的类CoursedDetails从类lay_main接收并显示数据。非常感谢jitendra,这向我发送了一个错误:无法从ArrayList强制转换到课程,为什么?你被覆盖了getItem方法吗非常感谢,它显然已经解决了,然而,我在稍后的CourseDetails.class中花了另一个问题,“未捕获处理程序:线程主线程由于未捕获异常而退出”现在是:“java.lang.RuntimeException:无法{启动activityComponentInfo com.mariposatraining.courses/com.mariposatraining.courses.CourseDetails}”:android.database.sqlite.SQLiteException:bind或column index超出范围:handle 0x2b6be8“但是我想我应该打开另一个问题来解决这个细节,显然我在sqlite中使用游标在该类中执行搜索时做了错误的语句!