获取片段中的语言环境
Get locale in fragment
所以我所做的是我创建了两个按钮,当按下一个按钮时,一个意图启动并且片段中的区域设置发生变化。我通过获取所需的语言环境值、将其转换为字符串并将其放入额外的 .它在 activites 之间工作得很好,但是当我将它设置为 fragment 时,它给了我一个错误
(java.lang.RuntimeException: Unable to start activity
ComponentInfo{.phraseDetailActivity}: java.lang.NullPointerException:
language=null,country=,variant=
这是我的代码。
activity发送额外的:
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
String changela = locale.getLanguage();
Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
i.putExtra("KEY",changela);
startActivity(i);
}
});
接收语言环境的片段:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
//error at the line under this comment
String changelee = getActivity().getIntent().getStringExtra("KEY");
Locale locale = new Locale(changelee);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config,
getActivity().getResources().getDisplayMetrics());
它给我这一行的错误。
String changelee = getActivity().getIntent().getStringExtra("KEY");
我也试过很多东西,比如把接收代码放到另一个地方,把它放在 activity 管理片段但是没用。
注意:显示在我的代码中。
我认为问题出在这一行 String
changelee = getActivity().getIntent().getStringExtra("KEY")
我应该改变一下它。
您是否尝试过覆盖 onConfigurationChanged() 以获取更新的区域设置?
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final Locale locale = newConfig.locale;
...
}
更新:
监听 LocalBroadcastManager 并在语言环境更改后通过 Intent 传递数据,
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String changela = getResources().getConfiguration().locale.getLanguage()
Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
i.putExtra("KEY",changela);
startActivity(i);
}
};
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
});
更新 2:
您从 Activity 发送数据,意图如下:
Bundle bundle = new Bundle();
bundle.putString("KEY", changela);
// set YourFragmentclass Arguments
YourFragmentclass fragobj = new YourFragmentclass ();
fragobj.setArguments(bundle);
并在 Fragment onCreateView 方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("KEY");
return inflater.inflate(R.layout.fragment, container, false);
}
所以我所做的是我创建了两个按钮,当按下一个按钮时,一个意图启动并且片段中的区域设置发生变化。我通过获取所需的语言环境值、将其转换为字符串并将其放入额外的 .它在 activites 之间工作得很好,但是当我将它设置为 fragment 时,它给了我一个错误
(java.lang.RuntimeException: Unable to start activity
ComponentInfo{.phraseDetailActivity}: java.lang.NullPointerException:
language=null,country=,variant=
这是我的代码。
activity发送额外的:
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
String changela = locale.getLanguage();
Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
i.putExtra("KEY",changela);
startActivity(i);
}
});
接收语言环境的片段:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
//error at the line under this comment
String changelee = getActivity().getIntent().getStringExtra("KEY");
Locale locale = new Locale(changelee);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config,
getActivity().getResources().getDisplayMetrics());
它给我这一行的错误。
String changelee = getActivity().getIntent().getStringExtra("KEY");
我也试过很多东西,比如把接收代码放到另一个地方,把它放在 activity 管理片段但是没用。
注意:显示在我的代码中。
我认为问题出在这一行 String changelee = getActivity().getIntent().getStringExtra("KEY") 我应该改变一下它。
您是否尝试过覆盖 onConfigurationChanged() 以获取更新的区域设置?
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final Locale locale = newConfig.locale;
...
}
更新:
监听 LocalBroadcastManager 并在语言环境更改后通过 Intent 传递数据,
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String changela = getResources().getConfiguration().locale.getLanguage()
Intent i = new Intent(Countrylist.this,PhraseDetailActivty.class);
i.putExtra("KEY",changela);
startActivity(i);
}
};
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
});
更新 2:
您从 Activity 发送数据,意图如下:
Bundle bundle = new Bundle();
bundle.putString("KEY", changela);
// set YourFragmentclass Arguments
YourFragmentclass fragobj = new YourFragmentclass ();
fragobj.setArguments(bundle);
并在 Fragment onCreateView 方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("KEY");
return inflater.inflate(R.layout.fragment, container, false);
}