我以编程方式创建一个ViewGroup。然后我添加两个视图,一个游戏和一个SeekBar。但是,如果我尝试在游戏中更改SeekBar属性(它有一个指向AndroidAdapter的指针),例如parent.seekBar.setVisibility(View.VISIBLE)
应用程序崩溃。
父级字段在游戏类中设置如下:
public class Simulation extends ApplicationAdapter implements GestureDetector.GestureListener{
AndroidLauncher parent=null;
public void setParent( AndroidLauncher nparent ){
parent = nparent;
}
public void something(){
if( parent != null ) parent.layout.removeView( parent.seekBar );
}
}
我错过了更改视图的权限吗?在编辑SeekBar之前,我是否设置/更改了某些内容?谢谢!
public class AndroidLauncher extends AndroidApplication {
Simulation simul;
ViewGroup layout;
SeekBar seekBar;
ShapeDrawable thumb;
LayoutParams lp;
@Override
protected void onCreate (Bundle savedInstanceState) {
simul = new Simulation();
simul.setParent(this);
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
layout = (ViewGroup) findViewById( R.id.panel );
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useWakelock = true;
config.useAccelerometer = false;
config.useCompass = false;
layout.addView(initializeForView(simul, config));
seekBar = new SeekBar(this);
seekBar.setMax(100);
thumb = new ShapeDrawable(new OvalShape());
thumb.setIntrinsicHeight(50);
thumb.setIntrinsicWidth(50);
seekBar.setThumb(thumb);
seekBar.setProgress(1);
seekBar.setBackground(new ColorDrawable(android.R.color.transparent));
lp = new LayoutParams(600, 50);
seekBar.setLayoutParams(lp);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
System.out.println(".....111.......");
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
System.out.println(".....222.......");
}
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
System.out.println(".....333......." + arg1);
}
});
layout.addView(seekBar);
}
}
答案 0 :(得分:0)
只有主线程可以编辑视图。要编辑其他类的视图,必须使用setParent(this)
之类的内容将引用传递给主线程Class。应在主线程Class中设置回调方法,当您希望编辑视图时,将调用此方法。在此方法中,您必须保证指令由主/ UI线程执行,您可以使用runOnUiThread(...)
来执行此操作。
检查以下示例:
public class AndroidLauncher extends AndroidApplication {
Simulation simul;
ViewGroup layout;
SeekBar seekBar;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
simul = new Simulation();
simul.setParent(this);
setContentView(R.layout.main);
layout = (ViewGroup) findViewById( R.id.panel );
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useWakelock = true;
config.useAccelerometer = false;
config.useCompass = false;
layout.addView(initializeForView(simul, config));
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
System.out.println( progress );
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void showSeekBar(){
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar.setVisibility( View.VISIBLE );
}
});
}
public void hideSeekBar(){
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar.setVisibility( View.GONE );
}
});
}
XML文件设置如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/main" android:orientation="vertical">
<FrameLayout
android:id="@+id/panel"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<SeekBar
android:layout_width="406dp"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_gravity="top|center_horizontal"
android:progress="50"
android:max="200"
android:visibility="gone"
android:layout_marginTop="30dp" />