我正在尝试编写一个包含一些视图的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();
}
答案 0 :(得分:5)
创建一个静态实例变量并在oncreate中设置:
private static myView instance;
的OnCreate()
instance = this;
static pickrandom()
instance.fillView();
答案 1 :(得分:0)
在pickRandom中,您尝试创建类的新实例。而不是这个,你应该做以下事情:
this.fillView();
我认为你的pickRandom没有任何静止的目的。
但是,如果出于某种原因需要它,可以像这样传递对视图的引用:
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();
}