使用共享首选项扩充视图
Inflate a view using shared pref
我必须自定义 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");
}
}
});
我使用
在主 activity 中调用该方法
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)
好的,你需要这样做:
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);
}
我必须自定义 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");
}
}
});
我使用
在主 activity 中调用该方法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)
好的,你需要这样做:
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);
}