在 listview 未捕获处理程序之后传递到另一个活动时出错:由于未捕获异常,线程主要退出

发布于 2025-01-07 13:14:56 字数 5212 浏览 1 评论 0原文

我有问题...

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);  
 }
}

我的类 CourseDetails 用于接收和显示数据:

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")));

    }


}}

我的适配器类:

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;
}
}

不是我做错了,真的很感谢你的帮助。

I have the issue...

02-20 02:57:09.854: E/AndroidRuntime(12431): Uncaught handler: thread main exiting due to uncaught exception"

...later...

02-20 02:57:09.864: E/AndroidRuntime(12431): java.lang.ClassCastException: java.util.ArrayList

...in a ListView populated with SQLite, the listView works fine, but later when i try to use onListItemClick, to get the parameters of the item on the list view selected and show it in other layout, the program stops and the DDMS says this "Uncaught handler: thread main exiting due to uncaught exception" later says "java.lang.ClassCastException: java.util.ArrayList" in a ListView" maybe i handled bad my adapter of the Arraylist?

This is my main class:

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);  
 }
}

my class CourseDetails for receive and show the data:

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")));

    }


}}

My Adapter class:

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;
}
}

not that I'm doing wrong, really would appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-01-14 13:14:56

重写适配器的 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);
    }
}

并在 itemClick 上进行如下更改:

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);  
 }

Override getItem method of adapter and return Cources Object from getItem,

Override
public Courses getItem(int position)
{
   return courses.get(positon);
}

and in SetupAdapter method:

public void setupAdapter()
{
if(myAdap.getCourses()!=null)
    {
    adapter = new  cAdapter(this, R.layout.list_courses, myAdap.getCourses());
    list.setAdapter(adapter);
    }
}

and change on itemClick as follows:

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);  
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文