gpt4 book ai didi

java - 单击android应用程序后访问ArrayList

转载 作者:行者123 更新时间:2023-12-01 15:27:04 26 4
gpt4 key购买 nike

我目前正在尝试在 Android 中创建 3D 游戏,但遇到了一些问题。

在点击游戏中的方向按钮后的某个时刻,我尝试访问 ArrayList,但似乎我的 ArrayList(之前已填充)现在为空。

这是线程问题吗?

@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
}
});

这是我的全部代码;

 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
}

最佳答案

第二种情况下的

thisOnClickListener 的实例,因此您需要完全限定它,或者完全删除 this,例如:

EnclosingClass.this.dataBase.get(0);

dataBase.get(0);

请注意,我以小写开头的变量名称,以符合 Java 的命名约定。

关于java - 单击android应用程序后访问ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com