我有四个图像视图,它们在活动开始时(或者更确切地说在行膨胀时)设置为不可见。我想按下一个按钮,让它们都变得可见(并且随后可以作为 onclicks 操作......)。我有以下摘要代码,但是每当我尝试访问除最后一个以外的任何其他图像视图时,我都会不断收到错误消息。
public class xx {
private ImageView up;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Log.i(TAG, "excersizeAdapter getView");
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Excersize e = items.get(position);
if (e != null) {
TextView exTitle = (TextView) v.findViewById(R.id.ex_type_title);
TextView exOn = (TextView) v.findViewById(R.id.ex_on_title);
TextView exRestAmount = (TextView) v.findViewById(R.id.ex_rest_amount);
TextView exSequence = (TextView) v.findViewById(R.id.ex_sequence);
up = (ImageView) v.findViewById(R.id.moveup);
down = (ImageView) v.findViewById(R.id.movedown);
if (exTitle !=null) {
exTitle.setText(e.getTypeTitle());
}
if (exOn !=null) {
exOn.setText("On: "+e.getTypeOn());
}
if (exRestAmount !=null) {
exRestAmount.setText(e.getRestAmount().toString());
}
if (exSequence !=null) {
exSequence.setText(e.getSequence().toString());
up.setTag(8881);
down.setTag(8882);
up.setId(((Integer)e.getSequence()));
down.setId(((Integer)e.getSequence()));
Log.i(TAG, "up,down values=" +e.getSequence());
}
}
return v;
}
}
public void toggleReorderButtons() {
up.findViewById(2).setVisibility(View.VISIBLE);
up.setVisibility(View.VISIBLE);
up.setOnClickListener(this);
}
}
好的,这是精简的代码,但是在 makevisinvis 方法中up.findview...
它会导致崩溃,并且 up.setvis 等将仅打开添加到我的活动中的最后一个。
我也尝试了以下但没有成功。
ImageView upper = (ImageView) up.findViewById(2);
upper.setVisibility(View.VISIBLE);
一个可爱的错误转储:
04-21 09:10:55.650: W/dalvikvm(22392): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-21 09:10:55.675: E/AndroidRuntime(22392): FATAL EXCEPTION: main
04-21 09:10:55.675: E/AndroidRuntime(22392): java.lang.NullPointerException
04-21 09:10:55.675: E/AndroidRuntime(22392): at com.mediabarltd.digittrainer.ProgramDisplay.toggleReorderButtons(ProgramDisplay.java:437)
04-21 09:10:55.675: E/AndroidRuntime(22392): at com.mediabarltd.digittrainer.ProgramDisplay.onClick(ProgramDisplay.java:391)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.view.View.performClick(View.java:2538)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.view.View$PerformClick.run(View.java:9152)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.os.Handler.handleCallback(Handler.java:587)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.os.Looper.loop(Looper.java:130)
04-21 09:10:55.675: E/AndroidRuntime(22392): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-21 09:10:55.675: E/AndroidRuntime(22392): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 09:10:55.675: E/AndroidRuntime(22392): at java.lang.reflect.Method.invoke(Method.java:507)
04-21 09:10:55.675: E/AndroidRuntime(22392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-21 09:10:55.675: E/AndroidRuntime(22392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-21 09:10:55.675: E/AndroidRuntime(22392): at dalvik.system.NativeStart.main(Native Method)
它需要一些认真的外观和对我正在做的事情的彻底改造。但...
基本上我遵循教程Android ListView 和 ListActivity,然后实现了Android ListView Adapter OnClickListener issue中描述的简洁“技巧” 。
(我真的很难接受这个。)