【问题标题】:Remove View from static Layout? (Nullpointer)从静态布局中删除视图? (空指针)
【发布时间】:2016-11-06 02:35:17
【问题描述】:

由于某种原因,我无法从静态 RelativeLayout 中删除视图。布局在活动内部是静态的,所以我可以从每个视图访问它。当我删除例如我的 IntroView 通过。 MainActivity.layout.removeView(this),一切正常,但是当我对我的 LevelView (MainActivity.layout.removeView(this)) 执行相同操作时,我得到一个 Nullpointer,LevelView(或布局?)= 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();
    }

}

【问题讨论】:

  • 把你的代码和.xml放在这里
  • 您可以简单地隐藏它而不是删除它以避免空异常。
  • 是的,我也有同样的想法,但是去掉它不是更好吗?以及如何正确隐藏它以使 onDraw 不刷新?
  • 顺便说一句,我不使用 .xml,一切都是程序化的
  • 此时看起来更像 Layout 为空。贴一些代码伙伴

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


【解决方案1】:

好的,我只是做了以下操作: this.setVisibility(this.GONE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多