1

I am trying to write an Activity that has some views, a fillView() method that sets the views (which is not static because it must utilize getContentResolver), and a static method that makes a random choice from a cursor and then runs the fillView() method.

I had problems with this due to fillView not being static and pickRandom being static, so I tried to initialzize an instance of the class, but now it crashes on the line instance.fillView();

Sample code below. Any help would be appreciated. Perhaps there is a much easier way to accomplish what I am trying to do.

Thanks, Josh

public class myView extends Activity implements OnClickListener {


@Override 
   public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.myView);

fillView();

    }


public void fillView(){

    //creates views, runs cursor and applies results to the view created

}

public static void pickRandom() {   


          // runs cursor, picks random entry, next I want to apply the result to 
          //  view, so I run...

        myView v = new myView();
        v.fillView();

        }
Josh
  • 2,685
  • 6
  • 33
  • 47

2 Answers2

5

Make a static instance variable and set in in oncreate:

private static myView instance;

oncreate()

instance = this;

static pickrandom()

instance.fillView();
vakio
  • 3,134
  • 1
  • 22
  • 46
0

in your pickRandom you try to create new instance of your class. Instead of this you should do the following:

this.fillView();

I don't see any purpose you have your pickRandom static.

If, however, you need it for some reason you can pass a reference to your view like this:

public static void pickRandom(myView v) {   


  // runs cursor, picks random entry, next I want to apply the result to 
  //  view, so I run...

  v.fillView();

}
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • This is difficult because I utilize the pickRandom method in other activities, and don't always have the views available to pass. If I try this.fillView(); it tells me "Cannot use this in a static context" – Josh Nov 24 '10 at 13:37