我编辑了我使用的最后一个代码和Krunal的答案。当我选择一个具有监听器的微调器项时,我仍然会崩溃。
我希望片段中的微调器能够让用户能够更改语言。
然而,当用户更改语言时,应用程序崩溃......所以问题应该来自我的微调器的监听器。我的微调器有效地显示了不同的项目
以下是代码:
public class GreetingsFragment extends Fragment {
public GreetingsFragment(){}
private Spinner spinner1 ;
Context thiscontext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
thiscontext = container.getContext();
View rootView = inflater.inflate(R.layout.fragment_greetings, container, false);
setSpinnerContent( rootView );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> id, View rootView,
int pos, long arg3) {
if (pos == 1) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected Spanish", Toast.LENGTH_SHORT)
.show();
setLocale("sp");
} else if (pos == 2) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected Japanese", Toast.LENGTH_SHORT)
.show();
setLocale("jp");
} else if (pos == 3) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected German", Toast.LENGTH_SHORT)
.show();
setLocale("de");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity().getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
private void setSpinnerContent( View rootView )
{
spinner1 = (Spinner) rootView.findViewById( R.id.languages_spinner );
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.LANG, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter( adapter );
}
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(getActivity(), GreetingsFragment.class);
refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refresh);
}
}
答案 0 :(得分:0)
据我所知,旋转器位置从0开始,而不是1,
你试试这个。
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (pos == 0) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected Spanish", Toast.LENGTH_SHORT)
.show();
setLocale("sp");
} else if (pos == 1) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected Japanese", Toast.LENGTH_SHORT)
.show();
setLocale("jp");
} else if (pos == 2) {
Toast.makeText(getActivity().getApplicationContext(),
"You have selected German", Toast.LENGTH_SHORT)
.show();
setLocale("de");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show();
}
});
第二件事是你试图将片段转换为活动,所以你得到了这个bug, 请尝试此代码,它将解决您的错误。