【问题标题】:Inflate a view using shared pref使用共享首选项膨胀视图
【发布时间】:2018-02-20 21:12:17
【问题描述】:

我必须自定义 XML 视图;一个称为按钮,另一个称为 main_alt。我想要实现的是,当共享首选项被勾选时,它会改变布局。

当用户第一次启动应用程序时,我希望它显示buttons.xml 在添加更改 XML 的设置之前,我是如何在 mainActivity.xml 中添加标签 <include>

我的共享首选项很简单并且有效:

cb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (cb6.isChecked()) {

            editor.putBoolean("alt_layout",true);
            editor.putBoolean("checkbo6state", true);
            editor.commit();
            Log.i("Alt_Layout: ", "Activated");

        }else{
            editor.putBoolean("alt_layout",false);
            editor.putBoolean("checkbox6state", false);
            editor.commit();

            Log.i("Alt_Layout: ","Deactivated");
        }
    }
});

我在我的主要活动中调用该方法

sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean alt_Layout = sharedpreferences.getBoolean("alt_layout",false);
if (alt_Layout== true){
    View view1;
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
    view1 = inflater.inflate(R.layout.main_alt,null);
    LinearLayout comp = (LinearLayout) findViewById(R.id.main);
    view1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    <--This is line 80-->

    comp.addView(view1);

} else {
    View view;
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.buttons,null);
    LinearLayout comp = (LinearLayout) findViewById(R.id.main);
    view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    comp.addView(view);
}

但我在 logcat 中收到此错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                                                  at com.example.harrops.h20droidapp2.MainActivity.onCreate(MainActivity.java:80)

【问题讨论】:

  • 你在哪里发起了比赛?
  • MainActivity 的第 80 行是什么?
  • 添加到第 80 行的代码中,谢谢@PaulMacGuiheen

标签: java android view sharedpreferences layout-inflater


【解决方案1】:

好的,你需要这样做:

LinearLayout comp = (LinearLayout) findViewById(R.id.main);
if (alt_Layout) {
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.id.main, comp, true);
} else {
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.buttons, comp, true);
}

【讨论】:

  • 因为我的主要活动是主要内容view @Jafer 谢谢队友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
相关资源
最近更新 更多