【问题标题】:Get locale in fragment在片段中获取语言环境
【发布时间】:2015-07-04 00:17:20
【问题描述】:

所以我所做的是我创建了两个按钮,当一个按钮被按下时,一个意图启动并且片段中的语言环境发生了变化。我通过获取我想要的语言环境值来做到这一点,将其转换为字符串并将其放入额外的字符串中。它在活动之间工作得非常好,但是当我将它设置为片段时,它给了我一个错误

(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") 我应该改变它。

【问题讨论】:

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


    【解决方案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:

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

    【讨论】:

    • 请检查更新后的答案。如果您仍然遇到问题,请尝试从您的片段中打印“changelee”,以确保传递的数据不为空。
    • 很高兴听到它确实解决了您的问题。祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多