我想使用数组在strings.xml
RecycleView
中显示来自multi ViewHolder
的数据,当我显示不包含array
的数据时可以,但是如果我使用{{1 }},我在数组代码中出现错误。不幸的是,该应用程序已停止。 array
是:
logcat
您能解决这个问题吗?
DoaPagi.java
07-27 23:49:58.466 8159-8159/com.alexzh.tutorial.multiplerowlayoutsrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexzh.tutorial.multiplerowlayoutsrecyclerview/com.alexzh.tutorial.multiplerowlayoutsrecyclerview.DoaSore}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.alexzh.tutorial.multiplerowlayoutsrecyclerview.DoaPagi.getData(DoaPagi.java:40)
at com.alexzh.tutorial.multiplerowlayoutsrecyclerview.DoaSore.onStart(DoaSore.java:28)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
07-27 23:54:58.846 8159-8159/? I/Process: Sending signal. PID: 8159 SIG: 9
AdapterDoa.java
public class DoaPagi extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doa_pagi);
}
@Override
protected void onStart() {
super.onStart();
AdapterDoa adapter = new AdapterDoa(DoaPagi.getData());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DoaPagi.this);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
}
public static Context context;
public static List<ModelDoa> getData() {
String[] data = context.getResources().getStringArray(R.array.doasore);
List<ModelDoa> list = new ArrayList<>();
list.add(new ModelDoa("London", ModelDoa.DOA_PAGI));
list.add(new ModelDoa("Amsterdam", ModelDoa.DOA_PAGI));
list.add(new ModelDoa("Berlin", ModelDoa.DOA_PAGI));
// error code is here
for (int i = 0; i < data.length; i++) {
list.add(new ModelDoa(data[i], ModelDoa.DOA_PAGI));
}
return list;
}
}
PagiViewHolder.java
public class AdapterDoa extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ModelDoa> mList;
public AdapterDoa(List<ModelDoa> list) {
this.mList = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case DOA_PAGI:
View vieu = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
PagiViewHolder rcv = new PagiViewHolder(vieu);
return rcv;
case DOA_SORE:
View doa = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
SoreViewHolder mdoa = new SoreViewHolder(doa);
return mdoa;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ModelDoa object = mList.get(position);
if (object != null) {
switch (object.getType()) {
case DOA_PAGI:
((PagiViewHolder) holder).mTitle.setText(object.getName());
break;
case DOA_SORE:
((SoreViewHolder) holder).mTitle.setText(object.getName());
break;
}
}
}
@Override
public int getItemCount() {
if (mList == null)
return 0;
return mList.size();
}
@Override
public int getItemViewType(int position) {
if (mList != null) {
ModelDoa object = mList.get(position);
if (object != null) {
return object.getType();
}
}
return 0;
}
}
答案 0 :(得分:0)
我相信问题出在这里:
String[] data = context.getResources().getStringArray(R.array.doasore);
您正在使用context
的{{1}}变量,因为尚未初始化。
一种可能的解决方案如下:
null
答案 1 :(得分:0)
将上面的代码更改为下面的代码,就可以了:
public static List<ModelDoa> getData() {
String[] data = getResources().getStringArray(R.array.doasore);
List<ModelDoa> list = new ArrayList<>();
list.add(new ModelDoa("London", ModelDoa.DOA_PAGI));
list.add(new ModelDoa("Amsterdam", ModelDoa.DOA_PAGI));
list.add(new ModelDoa("Berlin", ModelDoa.DOA_PAGI));
// error code is here
for (int i = 0; i < data.length; i++) {
list.add(new ModelDoa(data[i], ModelDoa.DOA_PAGI));
}
return list; }