0

I need to do setVisibility for an imageview named "heart" through handler and and function get(A). I declared imageview heart as a final variable in the onCreation method and under the class named Another Activity. But whenever I tried to set visibility for "heart", my application stopped with the log saying:

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)

Below is the abstract code that I erased unimportant part ( as I think..)

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
DroidDev
  • 1,527
  • 4
  • 20
  • 42
Sungpah Lee
  • 1,003
  • 1
  • 13
  • 31

1 Answers1

0

could you post the full error log?

why do you have 2 heart variable? and in getA() you are using 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

in the heart variable you will be referencing your image view widget and inside the handler i bet the imageview needs to be final? then pass it as parameter to the function to get access to it.

Ker p pag
  • 1,568
  • 12
  • 25
  • @user318605. Wow it works! I wonder why I have to make different final ImageView in the handler instead of putting it just under onCreation. Could you explain it to me? I'm just beginner of programming. – Sungpah Lee Jul 31 '14 at 06:26
  • you could accept my answer if it works. first is that if you make heart a final variable you cant reference it to your R.id.heart, because you cant change the value of a final variable, and you could access R.id.heart inside the onCreate() function. the one that you did. final ImageView heart = (ImageView) findViewById(R.id.heart); the final variable heart can only be accessed inside the onCreate() function. – Ker p pag Jul 31 '14 at 06:31