gpt4 book ai didi

java - fragment 启动时java.lang.NullPointerException的错误

转载 作者:行者123 更新时间:2023-11-29 20:05:41 24 4
gpt4 key购买 nike

我有包含三个不同 fragment 的选项卡。考勤、考试和成绩。在某些手机上,当结果 fragment 启动时,应用程序崩溃说。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

这是日志

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() 的第一行中 我已经尝试了很多方法来解决这个问题,但没有成功。如果你们帮我解决这个问题,那将非常有帮助。

最佳答案

你是在fragment中让它onCreate吗?

添加对 getContext()!=null 的检查

之前 block

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);
}

关于java - fragment 启动时java.lang.NullPointerException的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35681249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com