0

I know there are similar questions asked before in SO, but sorry to say that, none of them are serving my purpose.

I have a button in an activity class, and I want to give its functionality in another class.

Below is my HomeActivity code:

//  Tile Button
    tileButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TileButton tileView = new TileButton();
            tileView.tile();
        }
    });

And here is TileButton.java class code:

public class TileButton {
HomeActivity homeActivity = new HomeActivity();
View view = homeActivity.hometabView;
public void tile(){
    if(view.isShown()){
        view.setVisibility(View.INVISIBLE);
    }else{
        view.setVisibility(View.VISIBLE);
    }
}
}

Now when I press the tile button, a Null Pointer Exception is thrown. Below is the LogCat entry.

10-04 10:32:07.833: E/AndroidRuntime(5330): java.lang.NullPointerException

How do I solve this problem? Please help

User210282
  • 83
  • 4
  • 13
  • I suggest to save the state in `SharedPreference` and retrieve it in other activity. Make your view visible or invisible according to the status of preference. – Ravi Bhatt Oct 04 '13 at 05:31

2 Answers2

5

Change:

public class TileButton {

public void tile(View view){
    if(view.isShown()){
        view.setVisibility(View.INVISIBLE);
    }else{
        view.setVisibility(View.VISIBLE);
    }
}
}

//  Tile Button
    tileButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TileButton tileView = new TileButton();
            tileView.tile(v);// you can pass any view from here
        }
    });
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

If you want to have same operation in both the activities, Create a public method in one of the activity and just call the method onClick of both buttons. But you cannot control the visibility of an activity which is not even on screen.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • Both classes are NOT activities, only HomeActivity is an activity class. TileButton is just a plain java class which does the operation of hiding/unhiding stuffs. – User210282 Oct 04 '13 at 05:31
  • In that case you can give a command to hide/unhide view and then in onResume method of activity , check the command and hide/unhide accordingly. – Jitender Dev Oct 04 '13 at 05:33