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.