[英]Attempt to invoke virtual method ' 'on a null object reference
I am getting the following error我收到以下错误
Attempt to invoke virtual method 'void android.widget.StackView.setAdapter(android.widget.Adapter)' on a null object reference尝试在空对象引用上调用虚拟方法“void android.widget.StackView.setAdapter(android.widget.Adapter)”
on this line在这条线上
stackView.setAdapter(adapter);
The complete fragment EventsFragment.java is完整的片段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 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 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;
}
}
You are doing:你在做:
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
Your stackView
is null
.您的stackView
为null
。 getActivity().findViewById
returns null
. getActivity().findViewById
返回null
。
Why are you using getActivity()
?你为什么使用getActivity()
?
Where is the stackView
? stackView
在哪里? You should load it from the right xml.您应该从正确的 xml 加载它。
as @Tauqir mentioned, you need to inflate the right xml like this:正如@Tauqir 所提到的,您需要像这样膨胀正确的 xml:
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;
Try this inside onCreateView在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;
}
R.id.stackView1
当您尝试分配未找到stackView
,所以它是null
Do this做这个
View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview
You first need to inflate the layout and call findViewById() on the View it returns, thus getActivity().findViewById should be rootView.findViewByID.您首先需要膨胀布局并在它返回的视图上调用 findViewById(),因此 getActivity().findViewById 应该是 rootView.findViewByID。
There are some other issues with your code as well.您的代码还有一些其他问题。 Like getctivity().getApplicationContext, just getActivity() is fine.像 getctivity().getApplicationContext 一样,只需要 getActivity() 就可以了。
change container and false to null将容器和 false 更改为 null
Do this:做这个:
View rootView = inflater.inflate(R.layout.events, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.