1

我正在尝试将微调器更改为:按钮 > 对话框 > 微调器。

完美运行,但我看不到下面的消息(发生错误并关闭应用程序):

“您已选择英语 en-us”、“您已选择葡萄牙语 pt-br”

前:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

            if (pos == 0) { 
                Toast.makeText(parent.getContext(),
                        "You have selected English en-us", Toast.LENGTH_SHORT)
                        .show();
                setLocale("en");
            } else if (pos == 1) {
                 Toast.makeText(parent.getContext(),
                        "You have selected Português pt-br", Toast.LENGTH_SHORT)
                        .show();
                setLocale("br");
            } 
         }
         public void onNothingSelected(AdapterView<?> arg0)
        {
        }
     });
}

public void setLocale(String lang)
{
    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}

我的新代码:

Button bt_lang = (Button) findViewById(R.bt.language);
    bt_lang.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Select Language");
                builder.setAdapter(adapter, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        AdapterView<?> parent = null;
                        switch(which)
                        { 
                            case 0:
                            setLocale("en");
                            Toast.makeText(parent.getContext(),
                                    "You have selected English en-us", Toast.LENGTH_SHORT)
                                    .show();

                            case 1:
                            setLocale("br");
                            Toast.makeText(parent.getContext(),
                                    "Você selecionou Português pt-br", Toast.LENGTH_SHORT)
                                    .show();
                            break;
                        }
                        dialog.dismiss();
                    }
                });
                builder.create();
                builder.show();
        }
    });
}
    public void setLocale(String lang)
{
    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}
4

1 回答 1

0

你得到了一个 NPE,因为你初始化AdapterView<?> parent为 null,并且在使用它之前从来没有给它分配任何东西。无论如何,你不需要它作为上下文。在您的Toast.makeText()两种方法中,替换parent.getContext()MainActivity.this. Activity 是从上下文扩展而来的,因此您几乎总是可以将您的活动用作需要上下文的方法中的上下文参数。

于 2013-08-25T17:36:00.097 回答