首先,我想要实现的是:每当一个人点击一个特定的按钮(这个按钮在 Listview 行的每一行上),根据那个按钮的行应该改变颜色。
这是我迄今为止尝试过的,但没有成功,因为它给了我错误。
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageButton add_to_cart = (ImageButton) rowView.findViewById(R.id.add_to_cart_butt);
final ListView lv = (ListView) rowView.findViewById(R.id.product_listview);
final int thePosition = position;
add_to_cart.setClickable(true);
add_to_cart.setId(position);
add_to_cart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lv.setFocusable(true); // This is the line where error begins
lv.setBackgroundColor(Color.GREEN);
}
});
10-06 15:56:19.302: E/AndroidRuntime(1859): FATAL EXCEPTION: main
10-06 15:56:19.302: E/AndroidRuntime(1859): java.lang.NullPointerException
10-06 15:56:19.302: E/AndroidRuntime(1859): at classes.com.example.MyExample.itemAdapter$1.onClick(itemAdapter.java:66)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.view.View.performClick(View.java:4240)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.view.View$PerformClick.run(View.java:17721)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.os.Handler.handleCallback(Handler.java:730)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.os.Looper.loop(Looper.java:137)
10-06 15:56:19.302: E/AndroidRuntime(1859): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 15:56:19.302: E/AndroidRuntime(1859): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:56:19.302: E/AndroidRuntime(1859): at java.lang.reflect.Method.invoke(Method.java:525)
10-06 15:56:19.302: E/AndroidRuntime(1859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 15:56:19.302: E/AndroidRuntime(1859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 15:56:19.302: E/AndroidRuntime(1859): at dalvik.system.NativeStart.main(Native Method)
我该如何解决?谢谢。