我有三个不同片段的标签。出勤,考试和结果。在某些手机上,当结果片段启动时,应用程序崩溃说。
java.lang.NullPointerException:尝试调用虚方法 “java.lang.Object继承 在null上的android.content.Context.getSystemService(java.lang.String)' 对象参考
这里是logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:220)
at com.bu.innovate.bustudentportal.Result_Class$ExampleAdapter.<init>(Result_Class.java:886)
at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:544)
at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:245)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
它指向的代码就是这个
public class Result_Class extends Fragment implements java.io.Serializable {
private AnimatedExpandableListView listView;
private ExampleAdapter adapter;
String url;
String url2;
String url3;
static String urladder = "Transcript.aspx";
static String urladder2 = "Result.aspx";
static String urladder3 = "Result_Exam.aspx";
HashMap<String, String> hashMaps;
List<String> semester = new ArrayList<>();
List<Result_Gdata> GPA = new ArrayList<>();
List<Result_data> data_for_gra = new ArrayList<>();
List<Result_data> loop_finder = new ArrayList<>();
List<String> semester2 = new ArrayList<>();
List<Result_Gdata> GPA2 = new ArrayList<>();
List<Result_data> data_for_gra2 = new ArrayList<>();
List<Result_data> loop_finder2 = new ArrayList<>();
View rootview;
int num = 0;
HashMap<String, List<Result_data>> listDataChild;
FileOutputStream outputStream;
FileOutputStream outputStream2;
FileInputStream inputStream;
FileInputStream inputStream2;
String filename = "newResultData";
String filename2 = "oldResultData";
List<Result_data> data_for_gra3 = null;
List<String> semesters3 = null;
List<Result_data> loop_finder3 = null;
List<Result_Gdata> GPA3 = null;
List<Result_data> data_for_gra4 = null;
List<String> semesters4 = null;
List<Result_data> loop_finder4 = null;
List<Result_Gdata> GPA4 = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
super.onCreate(savedInstanceState);
rootview = inflater.inflate(R.layout.anim_result_layout, container, false);
listView = (AnimatedExpandableListView) rootview.findViewById(R.id.listView);
hashMaps = Data.map;
listView.setDivider(null);
listView.setDividerHeight(0);
if (Data.mychoice == 1) {
num = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
new result_getter_second().execute();
}
}, 1800);
} else {
Handler handlers2 = new Handler();
handlers2.postDelayed(new Runnable() {
@Override
public void run() {
new results().execute();
}
}, 2000);
}
return rootview;
}
private class result_getter_second extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
num = 0;
listDataChild = new HashMap<>();
List<GroupItem> items = new ArrayList<>();
if (semester.size() != 0) {
for (int m = 0; m < semester.size(); m++) {
GroupItem item = new GroupItem();
item.title = semester.get(m);
if (m < GPA.size()) {
item.gpa = GPA.get(m).getGPA();
item.cgpa = GPA.get(m).getCGPA();
}
List<Result_data> datw = new ArrayList<>();
for (int n = num; n < loop_finder.get(m).getSize_of_the_result(); n++) {
ChildItem child = new ChildItem();
child.course = data_for_gra.get(n).getSubject();
child.grade = data_for_gra.get(n).getGrade();
item.items.add(child);
datw.add(new Result_data(data_for_gra.get(n).getSubject(), data_for_gra.get(n).getGrade()));
num++;
}
items.add(item);
if(getActivity()!=null)
{
adapter = new ExampleAdapter(getActivity()); // line number 544
adapter.setData(items);
listView.setAdapter(adapter);
}
listView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (listView.isGroupExpanded(groupPosition)) {
listView.collapseGroupWithAnimation(groupPosition);
} else {
listView.expandGroupWithAnimation(groupPosition);
}
return true;
}
});
}
}
}
}
private static class GroupItem {
String title;
String gpa;
String cgpa;
List<ChildItem> items = new ArrayList<ChildItem>();
}
private static class ChildItem {
String course;
String grade;
}
private static class ChildHolder {
TextView course;
TextView grade;
}
private static class GroupHolder {
TextView title;
TextView gpa;
TextView cgpa;
}
/**
* Adapter for our list of {@link GroupItem}s.
*/
private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<GroupItem> items;
public ExampleAdapter(Context context) {
if(context!=null)
{
inflater = LayoutInflater.from(context); // line number 886
}
}
public void setData(List<GroupItem> items) {
this.items = items;
}
@Override
public ChildItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).items.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
ChildItem item = getChild(groupPosition, childPosition);
if (childPosition == 0) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
} else if (isLastChild) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result_last_child, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
} else {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result_middle, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
}
return convertView;
}
@Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).items.size();
}
@Override
public GroupItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
@Override
public int getGroupCount() {
return items.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
GroupItem item = getGroup(groupPosition);
if (groupPosition == getGroupCount() - 1) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.anim_group_item_result_last_child, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
convertView.setTag(holder);
holder.title.setText(item.title);
holder.gpa.setText(item.gpa);
holder.cgpa.setText(item.cgpa);
} else {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.anim_group_item_result, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
convertView.setTag(holder);
holder.title.setText(item.title);
holder.gpa.setText(item.gpa);
holder.cgpa.setText(item.cgpa);
}
if (groupPosition == 0) {
ExpandableListView elv = (ExpandableListView) parent;
elv.expandGroup(groupPosition);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
return (netInfo != null && netInfo.isConnected());
}
}
问题出现在调用getcontext()
的第一行我已经尝试了很多来解决这个问题但是却没有。如果你们帮助我解决这个问题,那将非常有帮助。
答案 0 :(得分:2)
你是否在片段中创建了它?
添加对getContext()的检查!= null
之前阻止
adapter = new ExampleAdapter(getContext());
adapter.setData(items);
listView.setAdapter(adapter);
最好替换getActivity()!= null
例如:
if (getActivity()!=null){
adapter = new ExampleAdapter(getActivity());
adapter.setData(items);
listView.setAdapter(adapter);
}