在自定义适配器中按钮单击中设置文本失败

时间:2015-08-11 15:00:25

标签: android textview custom-adapter

我点击了我的列表视图中的项目之间的按钮,一切正常,添加了一个将文本设置为TextView并失败的步骤。

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/txtOrden"
    android:layout_alignParentRight="true"
    android:layout_marginRight="23dp"
    android:background="@drawable/btn_green_small"
    android:maxHeight="48dp"
    android:maxWidth="80dp"
    android:shadowColor="#A8A8A8"
    android:text="Validar"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/txtMensaje"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:text="SDSDSDS"
    android:layout_marginRight="18dp"
    android:visibility="gone"
    android:textSize="10dp" />

自定义适配器中的代码:

TextView m = (TextView)v.findViewById(R.id.txtMensaje);
m.setVisibility(View.VISIBLE);
m.setText("E-ticket validado");

错误:

08-11 11:56:10.445: E/AndroidRuntime(28547): FATAL EXCEPTION: main
08-11 11:56:10.445: E/AndroidRuntime(28547): Process: com.eventrid.scanner, PID: 28547
08-11 11:56:10.445: E/AndroidRuntime(28547): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
08-11 11:56:10.445: E/AndroidRuntime(28547): at com.google.zxing.client.android.CustomListViewAdapter$1.onClick(CustomListViewAdapter.java:148)

奇怪的是,我正在使用一个按钮做同样的事情,但是如果有效的话。

Button b = (Button)v.findViewById(R.id.button1);
b.setBackgroundResource(R.drawable.icon_big_check);
b.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(params);

0 个答案:

没有答案