Android 是否从静态布局中删除视图?(空指针)
由于某些原因,我无法从静态RelativeLayout中删除视图。活动内部的布局是静态的,因此我可以从每个视图访问它。当我删除例如我的IntroView via时。MainActivity.layout.removeViewthis,一切正常,但当我对LevelView MainActivity.layout.removeViewthis执行相同操作时,我会得到一个空指针,即LevelView或布局?is=null,但这很奇怪,因为它在onCreate中初始化,就像IntroView一样,但出于某种原因,它在IntroView中工作 谢谢,我希望你明白我的意思Android 是否从静态布局中删除视图?(空指针),android,android-layout,android-activity,layout,view,Android,Android Layout,Android Activity,Layout,View,由于某些原因,我无法从静态RelativeLayout中删除视图。活动内部的布局是静态的,因此我可以从每个视图访问它。当我删除例如我的IntroView via时。MainActivity.layout.removeViewthis,一切正常,但当我对LevelView MainActivity.layout.removeViewthis执行相同操作时,我会得到一个空指针,即LevelView或布局?is=null,但这很奇怪,因为它在onCreate中初始化,就像IntroView一样,但出于
package catgames.lineland;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.azilen.locationbasedreminder.R;
import catgames.framework.Music;
import catgames.views.HudView;
import catgames.views.IntroView;
import catgames.views.Level1;
import catgames.views.Level2;
public class MainActivity extends Activity {
// Layout
public static RelativeLayout layout;
// Current Screen
public static String screen;
// Views
public static IntroView introview;
public static HudView hudview;
// Level
public static Level1 level1;
public static Level2 level2;
// Music
public static Music music;
// Context
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Context
context = this;
// Layout
layout = new RelativeLayout(this);
layout.setBackgroundResource(R.drawable.backdrop);
setContentView(layout);
// Level
introview = new IntroView();
hudview = new HudView();
level1 = new Level1();
level2 = new Level2();
// Screen
screen = "IntroView";
// Layout
layout.addView(introview);
layout.addView(hudview);
layout.addView(level1);
// Music
music = new Music(R.raw.music);
music.setLooping(true);
music.play();
}
}
好吧,我只是做了以下几点:
this.setVisibility this.GONE 将您的代码和.xml放在此处您可以简单地隐藏它,而不是删除它以避免空异常。是的,我也有同样的想法,但删除它对性能不是更好吗?我如何正确地隐藏它,使onDraw不会刷新?顺便说一句,我不使用.xml,所有内容都是编程的,此时布局看起来更像是空的。发布一些代码伙伴