0

I built language setting (uses activity class) and toolbar setting(uses appcompatactiviy). I want them be set oncreatemethod. But I cant extend two classes are activity and appcompatactivity. I extent them saparately on two classes (toolbar and SetupActivity). I called toolbar settings from toolbar class to set toolbar but compiler gave me an error. How can I achieve this without error. I would make mistake cause I am new on class topics. The code:

class toolbar extends AppCompatActivity{
@Override
public void setSupportActionBar(@Nullable Toolbar toolbar) {
    super.setSupportActionBar(toolbar);
}

@Nullable
@Override
public ActionBar getSupportActionBar() {
    return super.getSupportActionBar();
}
}

public class SetupActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Locale locale;
    if(getLocale()==null){
        locale = Locale.getDefault();
    }else{
        locale = new Locale(getLocale());
    }

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

    setContentView(R.layout.activity_setup);

    Toolbar setupToolBar= findViewById(R.id.setupToolbar);
    toolbar a = new toolbar();
    a.setSupportActionBar(setupToolBar);
    a.getSupportActionBar().setTitle(R.string.action_setting_text);
 }

  public String getLocale(){
    SharedPreferences prefs= getSharedPreferences("Settings", 
 Activity.MODE_PRIVATE);
    String language= prefs.getString("My_Lang", "");
    return language;
}

The error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tower935.blog/com.tower935.blog.SetupActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference. Please help me for this issue. Thanks.

Baris
  • 31
  • 5

1 Answers1

0

You may be getting NPException because some components have not been initialized.

Try setting setContentView(R.layout.activity_setup);

just after super.onCreate(savedInstanceState);

like so:

@override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);

    // Rest of the code goes here
}
denniz crypto
  • 451
  • 4
  • 10
  • I already set setContentView, it must be after locale for setting language. You will see setContentView by controlling my post again. – Baris Apr 19 '20 at 13:26