通过处理程序和函数设置图像可见性

时间:2014-07-31 05:56:02

标签: java android imageview handler final

我需要为名为" heart"的imageview执行setVisibility。通过处理程序和函数get(A)。 我将imageview heart声明为onCreation方法中的最终变量,并命名为Another Activity。但每当我试图设置" heart"的可见性时,我的应用程序停止了日志说:

07-31 01:29:49.740: E/AndroidRuntime(2689):     at org.voca.AnotherActivity.getEllapse(AnotherActivity.java:1559)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at org.voca.AnotherActivity.access$0(AnotherActivity.java:1531)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at org.voca.AnotherActivity$1.handleMessage(AnotherActivity.java:1519)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at android.os.Looper.loop(Looper.java:137)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at android.app.ActivityThread.main(ActivityThread.java:4998)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at java.lang.reflect.Method.invoke(Method.java:515)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-31 01:29:49.740: E/AndroidRuntime(2689):     at dalvik.system.NativeStart.main(Native Method)

下面是我删除不重要部分的抽象代码(我认为......)

public class AnotherActivity extends Activity {
ImageView heart;
public void onCreation(Bundle savedInstanceState) {
...
final ImageView heart = (ImageView) findViewById(R.id.heart);
 Start.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
       }});
   }//onCreation close
 Handler mTimer = new Handler() {
        public void handleMessage(Message msg) {
            OUTPUTA.setText(getA());
        }
    };
private String getA() {
       heart6.setVisibility(ImageView.INVISIBLE);
       ...
}
}//close AnotherActivity

1 个答案:

答案 0 :(得分:0)

你可以发布完整的错误日志吗?

为什么你有2个心脏变量?在getA()中你使用的是heart6?

public class AnotherActivity extends Activity {
ImageView heart;
public void onCreation(Bundle savedInstanceState) {
...
heart = (ImageView) findViewById(R.id.heart);
 Start.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
       }});
   }//onCreation close
 Handler mTimer = new Handler() {
        public void handleMessage(Message msg) {
            final ImageView visibleheart=heart;
            OUTPUTA.setText(getA(visibleheart));
        }
    };
private String getA(ImageView visibleheart) {
       visibleheart.setVisibility(ImageView.INVISIBLE);
       ...
}
}//close AnotherActivity

在heart变量中你将引用你的图像视图小部件,在处理程序内我敢打赌imageview需要是最终的吗?然后将其作为参数传递给函数以获取对它的访问权。