获取片段中的区域设置

时间:2015-07-03 14:27:56

标签: java android android-fragments android-intent locale

所以我做的是创建了两个按钮,当按下一个按钮时,意图启动并且片段中的区域设置发生变化。我通过获取我想要的语言环境值,将其转换为字符串并将其放入额外值来实现。它在活动之间完美地运行,但是当我将它设置为片段时,它给了我一个错误

(java.lang.RuntimeException: Unable to start activity   
ComponentInfo{.phraseDetailActivity}: java.lang.NullPointerException: 
language=null,country=,variant= 

这是我的代码。

发送额外的活动:

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

我也尝试过很多东西,比如将回收代码放到另一个地方,把它放在管理片段的活动中,但是没有用。

注意:在我的代码上显示。

我认为问题出在这一行String中     changelee = getActivity()。getIntent()。getStringExtra(“KEY”) 我应该改变一些事情。

1 个答案:

答案 0 :(得分:1)

您是否尝试重写 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:

从活动中您按意图发送数据:

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