从静态布局中删除视图? (空指针)

时间:2016-07-04 11:16:39

标签: android android-layout android-activity layout view

由于某种原因,我无法从静态RelativeLayout中删除视图。布局在活动内部是静态的,因此我可以从每个View访问它。当我删除例如我的IntroView通过。 MainActivity.layout.removeView(this),一切正常,但是当我对我的LevelView(MainActivity.layout.removeView(this))做同样的事情时,我得到一个Nullpointer,LevelView(或Layout?)是= null,但那是奇怪,因为它在onCreate中被初始化,就像IntroView一样,但由于某种原因它在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();
    }

}

1 个答案:

答案 0 :(得分:0)

好的,我刚刚做了以下事情: this.setVisibility(this.GONE);