我有一个数据库。数据库中的值显示在列表视图中。列表视图有一个位图、4 个文本视图和 2 个按钮。我为我的 2 个按钮编写了两个不同的 OnClickListener,但是这两种方法都不成功。当我单击按钮时,程序会崩溃,当它尝试执行“tvText1.setText(...)”时
有人知道如何解决吗?
xml 文件中的按钮看起来像:
<Button android:id="@+id/buttonON"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:onClick="btnButtonONclicked"
android:text="ON" />
<Button android:id="@+id/buttonOFF"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="OFF" />
这是我的光标适配器
public class ModuleCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
public ModuleCursorAdapter(Context context, Cursor cur) {
super(context, R.layout.notes_row, cur);
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = LayoutInflater.from(context);
return li.inflate(R.layout.notes_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
TextView tvText2 = (TextView)view.findViewById(R.id.text2);
TextView tvText3 = (TextView)view.findViewById(R.id.text3);
TextView tvText4 = (TextView)view.findViewById(R.id.text4);
ImageView ivIcon = (ImageView)view.findViewById(R.id.icon);
Button btnButtonON = (Button)view.findViewById(R.id.buttonON);
Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);
tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
tvText2.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_DEVICETYPE)));
tvText3.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_HOMECODE)));
tvText4.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_DEVICECODE)));
String filename = "modul_r_" + cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_DEVICETYPE));
int id = context.getResources().getIdentifier(filename, "drawable", "com.module.main");
ivIcon.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), id));
btnButtonON.setTag(id);
btnButtonOFF.setTag(id);
btnButtonOFF.setOnClickListener(btnButtonOFFclicked);
}
@Override
public void onClick(View view) {
}
public void btnButtonONclicked(View view) {
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
tvText1.setText("On");
}
private OnClickListener btnButtonOFFclicked = new OnClickListener() {
@Override
public void onClick(View view) {
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
tvText1.setText("Off");
}
};
}
谢谢您的帮助。
干杯
菲利克斯
评论。
整个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:scaleType="centerCrop"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:src="@drawable/my_icon"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:textSize="22dp"
android:layout_width="170dp"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView android:text="Code: "
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/text2"
android:textSize="15dp"
android:paddingLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/text3"
android:textSize="15dp"
android:paddingLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/text4"
android:textSize="15dp"
android:paddingLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<Button android:id="@+id/buttonON"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:onClick="btnButtonONclicked"
android:text="ON" />
<Button android:id="@+id/buttonOFF"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="OFF" />
评论评论:
现在我删除了 tv.Text1.setText("On")。对于 On-button 仍然有一个例外。对于关闭按钮,system.out.println() 运行良好。
这是代码的一部分:
public void btnButtonONclicked(View view) {
// Still an exeption thrown
}
private OnClickListener btnButtonOFFclicked = new OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("Hello. Here is the message.");
// working fine
}
};