我收到以下错误
尝试调用虚方法'void android.widget.StackView.setAdapter(android.widget.Adapter)'为null 对象参考
在这一行
stackView.setAdapter(adapter);
完整的片段 EventsFragment.java 是
public class EventsFragment extends android.support.v4.app.Fragment {
private StackView stackView;
private ArrayList<Stack_Items> list;
TypedArray eventLogo ;
String eventName[];
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
eventLogo= getResources().obtainTypedArray(R.array.event_stack_icon);
eventName = getResources().getStringArray(R.array.event_stack);
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();
//Adding items to the list
for (int i = 0; i < eventLogo.length(); i++) {
list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i,-1)));
}
//Calling adapter and setting it over stackView
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list );
stackView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.events_layout, null);
}
}
Stack_Adapter.java
public class Stack_Adapter extends BaseAdapter {
ArrayList<Stack_Items> arrayList;
LayoutInflater inflater;
ViewHolder holder = null;
public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
this.arrayList = arrayList;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Stack_Items getItem(int pos) {
return arrayList.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.stack_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.textView1);
holder.image = (ImageView) view.findViewById(R.id.imageView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(arrayList.get(pos).getName());
holder.image.setBackgroundResource(arrayList.get(pos).getImage());
return view;
}
public class ViewHolder {
TextView text;
ImageView image;
}
}
Stack_Items
public class Stack_Items {
String name;
Integer image;
public Stack_Items(String name, Integer image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}
}
答案 0 :(得分:3)
你在做:
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
您的stackView
是null
。 getActivity().findViewById
会返回null
。
您为什么使用getActivity()
?
stackView
在哪里?你应该从正确的xml加载它。
正如@Tauqir所提到的,你需要像这样膨胀正确的xml:
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;
答案 1 :(得分:1)
在 onCreateView
中尝试此操作 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.events_layout, null);
-------------some codes ------------
stackView = (StackView) view.findViewById(R.id.stackView1);
-------------some codes ------------
return view;
}
答案 2 :(得分:0)
R.id.stackView1
时找不到 stackView
,因此它是null
答案 3 :(得分:0)
这样做
View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview
首先需要对布局进行膨胀并在它返回的View上调用findViewById(),因此getActivity()。findViewById应该是rootView.findViewByID。
您的代码也存在其他一些问题。就像getctivity()。getApplicationContext一样,只需getActivity()就好了。
答案 4 :(得分:0)
将容器更改,将false更改为null
执行此操作:
View rootView = inflater.inflate(R.layout.events, null);