I'm currently trying to create a 3d game in android and I'm struggling with some issues.
At some point after clicking on a direction button in my game, I try to access an ArrayList but it seems that my ArrayList(that was filled earlier) is now empty.
Is that a threading issue?
@Override
public void Update(ArrayList<ArrayList<IModel>> Database) {
this.DataBase = Database;
System.out.println("---> " + this.DataBase.get(0)); //WORKING
}
Button up_btn = (Button) findViewById(R.joystick.up);
up_btn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
System.out.println("--------->" + DataBase.get(0)); //NOT WORKING
}
});
here's my whole code ;
public class PlayerController extends Activity implements IController
{
//Properties
private ArrayList<ArrayList<IModel>> DataBase = null;
private GLSurfaceView mGLView;
//Virtual
@Override public void Update(ArrayList<ArrayList<IModel>> Database)
{
this.DataBase = Database;
System.out.println("---> " + this.DataBase.get(0)); //WORKING
}
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.mGLView = new MySurfaceView(this);
LayoutInflater inflater = getLayoutInflater();
this.getWindow().addContentView(this.mGLView, new ViewGroup
.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
this.getWindow().addContentView(inflater.inflate(R.layout.footer, null), new ViewGroup
.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
this.getWindow().addContentView(inflater.inflate(R.layout.play, null), new ViewGroup
.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, (int)80));
//Joystick buttons
Button up_btn = (Button) findViewById(R.joystick.up);
up_btn.setOnClickListener(new View.OnClickListener()
{
@Override public void onClick(View v)
{
System.out.println(">>>>>" + DataBase.get(0)); //NOT WORKING..
//UpMotionListener();
}
});
}
@Override protected void onPause()
{
super.onPause();
mGLView.onPause();
}
@Override protected void onResume()
{
super.onResume();
mGLView.onResume();
}
public void UpMotionListener()
{
//System.out.println(">>>>>>>>>" + DataBase.get(0)); //NOT WORKING
}
this
in the second case is the instance of the OnClickListener
, so you need to fully qualify it, or remove this
at all, e.g.:
EnclosingClass.this.dataBase.get(0);
or
dataBase.get(0);
Please note that I started the variable name with lower case, to match Java's naming conventions.
Thank you anyway for your answe - user1316055 2012-04-05 21:11
Well, really your problem is that you don't understand the Java language, however that is fixable. Here is a quick overview of the specific knowledge you are missing: http://www.oursland.net/tutorials/java/innerclasses/
Now, to answer your question directly:
Try referring to Database without the "this." in front of it.
this.DataBase.get(0) in your new View.OnClickListener() is referring to your anonymous inner class created when you said new OnClickListener, not the instance class where you created you Database member. When you use "this" you are saying this class and since this is inside your listener it is referring to the class created inline and the "Database" member variable is out of scope.
Could it be that something else is modifying/clearing the ArrayList in-between the time Update and onClick are called?