我是一名新手安卓开发者。我想要改变我的RecyclerView列表项的语言。我的列表项存储在values文件夹下的字符串数组中。我该如何在单击列表项时更改其语言?以下是我尝试过的代码,但出现了错误。请问有人能够纠正我的错误或向我展示实现所需结果的正确方法吗?
错误信息:
错误信息:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.example.chaitanya.kundli.MainActivity.setLocale(MainActivity.java:60)
at com.example.chaitanya.kundli.RecyclerViewAdapter$1.onClick(RecyclerViewAdapter.java:66)
at android.view.View.performClick(View.java:4759)
at android.view.View$PerformClick.run(View.java:19770)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
MainActivity.java
public class MainActivity extends AppCompatActivity {
android.content.res.Resources res;
RecyclerView recyclerView;
Context context;
String[] numbers;
RecyclerView.Adapter recyclerView_Adapter;
RecyclerView.LayoutManager recyclerViewLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = getResources();
numbers = res.getStringArray(R.array.abc);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
context = getApplicationContext();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
//Change 2 to your choice because here 2 is the number of Grid layout Columns in each row.
recyclerViewLayoutManager = new GridLayoutManager(context, 2);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView_Adapter = new RecyclerViewAdapter(context,numbers);
recyclerView.setAdapter(recyclerView_Adapter);
}
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
String[] values;
Context context1;
public RecyclerViewAdapter(Context context2,String[] values2){
values = values2;
context1 = context2;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView) v.findViewById(R.id.textview1);
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view1 = LayoutInflater.from(context1).inflate(R.layout.recycler_view_items,parent,false);
ViewHolder viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
@Override
public void onBindViewHolder(ViewHolder Vholder,final int position){
Vholder.textView.setText(values[position]);
Vholder.textView.setBackgroundColor(Color.parseColor("#ffc947"));
Vholder.textView.setTextColor(Color.BLACK);
Vholder.textView.setBackgroundResource(R.drawable.rounded_corner);
Vholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity aa = new MainActivity();
if(position == 4){
aa.setLocale("hi");//calling function
}
}
});
}
@Override
public int getItemCount(){
return values.length;
}
}
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">nnn</string>
<string-array name="abc">
<item>Query</item>
<item>Answers</item>
<item>Rate Us</item>
<item>Change Language</item>
<item>Logout</item>
</string-array>
</resources>
string.xml(hi)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">nnn</string>
<string-array name="abc">
<item>सवाल पूछें </item>
<item>उत्तर देखें</item>
<item>रेटिंग दीजिये</item>
<item>भाषा बदलें</item>
<item>बाहर निकलें</item>
</string-array>
</resources>