"不能使静态引用非静态方法" Android中 [英] "Cannot make a static reference to a non-static method" in Android
问题描述
我在与老一些问题不能使静态引用非静态方法的错误在我的Android程序。我创建一个沙子掉落游戏(类似火药游戏)和我创建了一个名为控件创建一个控制栏,在与画笔大小滑块(即正常工作)屏幕的底部和一个按钮,弹出一个对话框,允许用户选择所选择的元素。然而,当我打电话DemoActivity.showDialog(2)从我的code,它给人的静态引用非静态误差(DemoActivity是我的应用程序的主要活动)。我也试着改变它只是Activity.showDialog(2),但我得到了完全同样的错误!请帮帮忙,我究竟做错了什么?这是我的code和在此先感谢:
I'm having some issues with the old "Cannot make a static reference to a non-static method" error in my Android program. I am creating a sand falling game (similar to the Powder Game) and I created a class called Control to create a Control Bar at the bottom of the screen with a slider for brush size (that works fine) and a button to pop up a Dialog to allow users to pick the selected element. However, when I call DemoActivity.showDialog(2) from my code, it gives the static reference to non-static error (DemoActivity is the main activity of my application). I also tried changing it to just Activity.showDialog(2), but I got exactly the same error! Please help, what am I doing wrong? Here's my code and thanks in advance:
package sand.falling.opengl;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class Control extends LinearLayout
{
private ImageButton control_button;
private SeekBar brush_size_slider;
final CharSequence[] elementslist = {"Sand", "Water", "Plant", "Wall", "Fire", "Ice", "Generator", "Oil", "Magma", "Stone", "C4"};
public Control(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onFinishInflate()
{
control_button = (ImageButton) findViewById(R.id.element_picker_button);
brush_size_slider = (SeekBar) findViewById(R.id.brush_size_slider);
control_button.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
//THIS DOESN'T WORK!!!!
DemoActivity.showDialog(2); //Run the element picker dialog
}
}
);
control_button.setImageResource(R.drawable.palette);
brush_size_slider.setOnSeekBarChangeListener
(
new SeekBar.OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromTouch)
{
int p = 32 * progress/100;
DemoActivity.setBrushSize(p);
Log.v("DemoActivity", "size:" + p);
}
public void onStartTrackingTouch(SeekBar seekbar) {}
public void onStopTrackingTouch(SeekBar seekbar) {}
}
);
brush_size_slider.setProgress((int)400/32);
}
}
编辑:我固定它通过添加以下到我的Control.java code:
I fixed it by adding the following to my Control.java code:
public class Control extends LinearLayout
{
private DemoActivity activity;
...
public void setActivity(DemoActivity act)
{
activity = act;
}
...
//Set a click listener for the button which should pop up element picker dialog when clicked
control_button.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
activity.showDialog(2); //Run the element picker dialog
}
}
);
}
,然后调用 control.setActivity(本);
从我的onResume DemoActivity.java的部分!希望它可以帮助那些有类似问题!
And then calling control.setActivity(this);
from my onResume section of DemoActivity.java! Hope it helps those of you with similar issues!!
推荐答案
您必须调用的ShowDialog
在 DemoActivity
实例,而不是类本身。你可以调用的唯一时间 ClassName.methodName()
是,如果该方法被定义为静态的。 的ShowDialog
不是一个静态方法。
You have to call showDialog
on a DemoActivity
instance, NOT on the class itself. The only time you can call ClassName.methodName()
is if the method is defined as static. showDialog
is not a static method.
要解决这个问题,您可能需要实例化一个新的 DemoActivity
或让现有的,然后调用的ShowDialog
在这一点。
To fix this, you either need to instantiate a new DemoActivity
or get an existing one, then call showDialog
on that.
编辑:如果您已经有一个 DemoActivity
例如,当你实例化这个控制
对象,或许下面的修改将工作:
If you already have a DemoActivity
instance when you instantiate this Control
object, perhaps the following modification will work:
public class Control extends LinearLayout
{
...
// add an Activity instance
private Activity activity;
// set the Activity in your constructor
public Control(Context context, AttributeSet attrs, Activity activity)
{
super(context, attrs);
this.activity = activity;
}
@Override
protected void onFinishInflate()
{
...
// Use the instance activity here
activity.showDialog(2);
...
}
}
这篇关于"不能使静态引用非静态方法" Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!