另一个"无法制作静态参考......"的问题

Jos*_*osh 1 static android

我正在尝试编写一个具有一些视图的Activity,一个设置视图的fillView()方法(因为它必须使用getContentResolver而不是静态的),以及一个从游标中随机选择然后运行fillView的静态方法() 方法.

由于fillView不是静态的而且pickRandom是静态的,所以我遇到了这个问题,所以我尝试初始化类的一个实例,但现在它在行instance.fillView()上崩溃了;

示例代码如下.任何帮助,将不胜感激.也许有一种更容易的方法来完成我想要做的事情.

谢谢,乔希

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();

        }
Run Code Online (Sandbox Code Playgroud)

vak*_*kio 5

创建一个静态实例变量并在oncreate中设置:

private static myView instance;
Run Code Online (Sandbox Code Playgroud)

在OnCreate()

instance = this;
Run Code Online (Sandbox Code Playgroud)

静态pickrandom()

instance.fillView();
Run Code Online (Sandbox Code Playgroud)