我正在尝试访问 setOnItemClick 侦听器,它会引发异常。我在其中添加了一个 try catch,并在 logcat 异常中登录。我正在做的事情是否导致它抛出异常?
这是自定义适配器:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StudentAdapter extends ArrayAdapter<Student> {
Context context;
int layoutResourseId;
Student data[] = null;
public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourseId = layoutResourceId;
this.data = data;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
StudentHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourseId, parent, false);
holder = new StudentHolder();
//Associate the Items in the holder with a view in the XML file.
holder.tvStNumber = (TextView)row.findViewById(R.id.tvStNumber);
row.setTag(holder);
} else
{
holder = (StudentHolder) row.getTag();
}
//Setting the text for the items
Student item =data[position];
holder.tvStNumber.setText(item.GetStudentNumber());
return row;
}
//Student Holder
static class StudentHolder
{
TextView tvStNumber;
TextView tvStDescription;
}
}
这是监听器方法:
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
try{
String listValue = ((TextView)view).getText().toString(); //This is where the exception is happening. The below text should have been commented out.
// String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening.
Toast.makeText(getApplicationContext(), "You click" + position, Toast.LENGTH_LONG).show();
String d = "Test toast.";
}catch(Exception e)
{
Log.e("Error Message", e.getMessage());
}
}
});
这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"
android:orientation="vertical"
tools:ignore="HardcodedText"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/linearylayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ListView
android:id="@+id/lvStudent"
android:layout_width="match_parent"
android:layout_height="144dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
这就是该行的外观。
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusable="false"
>
<TextView
android:id="@+id/tvStNumber"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="5sp"
android:text="EE#" />
<TextView
android:id="@+id/tvStDescription"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10sp"
android:layout_toRightOf="@+id/tvStNumber"
android:text="Item Description" />
</RelativeLayout>