I am having problems with the old error โI canโt make a static link to a non-static methodโ in my Android program. I am creating a game with falling sand (similar to the Powder game) and I created a class called Control to create a control panel at the bottom of the screen with a slider for the brush size (which works fine) and a popup button A dialog allowing users to select a selected item . However, when I call DemoActivity.showDialog (2) from my code, it gives a static link to non-static errors (DemoActivity is the main action of my application). I also tried changing it to Activity.showDialog (2), but I got exactly the same error! Please help, what am I doing wrong? Here is 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)
{
DemoActivity.showDialog(2);
}
}
);
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);
}
}
EDIT: 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;
}
...
control_button.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
activity.showDialog(2);
}
}
);
}
control.setActivity(this); onResume DemoActivity.java! , !