简体   繁体   中英

Inflate a view using shared pref

I have to custom XML views; one called buttons and the other called main_alt. What I'm trying to achieve is when the shared pref is ticked it changes the layout.

When the user first starts the app I want it to display the buttons.xml how I did this before adding the setting to change XML was to add the tag <include> in mainActivity.xml .

My shared pref is simple and works:

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");
        }
    }
});

I call the method in my main activity using

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);
}

but I get this error in my 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)

Ok, you need do this:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM