CursorAdapter 和按钮
我有一个数据库。数据库中的值显示在列表视图中。列表视图有一个位图、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" />
这是我的 CursorAdapter
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")。对于开机按钮仍有一个例外。对于“关闭”按钮,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
}
};
I have a database. The values from the database are displayed in a listview. The listview has a Bitmap, 4 TextViews and 2 Buttons.
I wrote two different OnClickListeners for my 2 Buttons, but both methods are not succesful. When I click on the buttons the program chrashes, when it tries to do "tvText1.setText(...)"
Has anybody an idea how to fix it?
The buttons in the xml-file does look like:
<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" />
This is my CursorAdapter
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");
}
};
}
Thank you for your help.
Cheers
Felix
Comment.
The whole Layout-file
<?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" />
Comment Comment:
Now I deleted the tv.Text1.setText("On"). For the On-button there is still a exeption. For the Off-button is the system.out.println() working well.
This is the part of the code:
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
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 TextView 位于哪里?或者也许更好:您能给我们展示您的整个布局文件吗?
两个 OnClickListener 都传递了一个 View 引用,但是正如您可以在文档中阅读的那样< /a> 参数是被单击的视图 - 换句话说:它将是对按钮的引用。这些按钮肯定不包含您要查找的文本视图,因此
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
不会产生任何结果被发现且
tvText1
为null
。这就是你的空指针的来源以及你的应用程序崩溃的原因。// 编辑:一些代码来说明下面评论中提到的内容。
顺便说一句,如果您正在寻找一个可以跟踪其状态的按钮,请查看
ToggleButton
,可设置为“on”和“off”Where are your TextViews located? Or perhaps better: can you show us your whole layout file?
Both OnClickListeners are passed a View reference, but as you can read in the documentation the argument is the view that was clicked - in other words: it'll be a reference to the Buttons. The buttons will definitely not contain the textviews you're looking for, hence
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
will result in nothing being found and
tvText1
to benull
. That's where your nullpointer is coming from and why your app crashes.// Edit: some code to illustrate what's mentioned in the comments below.
By the way, if you're looking for a button that keeps track of it's state, have a look at
ToggleButton
, which can be set to 'on' and 'off'尝试以下操作
Try the following
将 tvText1 设为 Button onClick Listener 的类级别字段
Make tvText1 as the class level field for Button onClick Listener
保留对父级的引用,以便稍后使用。
所以..
然后
Hold a reference to the parent which you can use later.
so..
and then