我希望在Api大于17时使用方法重置setContentView中的视图:setLayoutDirection但是出现了这个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutDirection(int)' on a null object reference
以下代码有什么问题。 Tnx提前。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if(Build.VERSION.SDK_INT>=17)
{
Locale en = new Locale("e@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if(Build.VERSION.SDK_INT>=17)
{
Locale en = new Locale("fa");
Locale.setDefault(en);
Configuration config = new Configuration();
config.locale = en;
getApplicationContext().getResources().updateConfiguratio n(config,getApplicationContext().getResources().getDisplayMetrics());
ViewCompat.setLayoutDirection(findViewById(R.id.setting_listview_brightness),ViewCompat.LAYOUT_DIRECTION_RTL);
setContentView(R.layout.activity_settings);
答案 0 :(得分:0)
请使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT >17)
setContentView(R.layout.your_layout);
}else {
setContentView(R.layout.your_layout_before_17);
}
//Do your other stuff.
}
希望这有帮助
答案 1 :(得分:0)
就这样做
if(Build.VERSION.SDK_INT>=17)
{
Locale en = new Locale("fa");
Locale.setDefault(en);
Configuration config = new Configuration();
config.locale = en;
getApplicationContext().getResources().updateConfiguratio n(config,getApplicationContext().getResources().getDisplayMetrics());
ViewCompat.setLayoutDirection(findViewById(R.id.setting_listview_brightness),ViewCompat.LAYOUT_DIRECTION_RTL);
setContentView(R.layout.your_layout_for17);
}