Мне нужно настраивать XML-представления; один называется кнопками, а другой называется main_alt. Чего я пытаюсь добиться, так это того, что когда общий префикс отмечен галочкой, он меняет макет.
Когда пользователь впервые запускает приложение, я хочу, чтобы оно отображало кнопки. XML, как я это сделал, прежде чем добавлять параметр для изменения XML, заключался в добавлении тега <include>
в mainActivity.xml
.
Мой общий префикс прост и работает:
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);
}
но я получаю эту ошибку в своем логарифме
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)