我目前有一个包含3个标签的活动,每个标签包含它自己的片段。就布局而言,第一和第二标签相对相同。每当活动加载时,第一个选项卡都会加载,但是当我切换到第二个选项卡时,我立即得到一个NullPointerException。这是第二个选项卡的代码
StudentFragmentTab2
[package]
[imports]
public class StudentFragmentTab2 extends Fragment {
View view;
LinearLayout layout;
ListView listview;
String Student_JSON;
JSONArray examArray;
ExamObject exams;
ArrayList<ExamObject> list = new ArrayList<ExamObject>();
public static loginFunctions session = new loginFunctions();
AdapterView.AdapterContextMenuInfo info;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_student_tab1, container, false);
Bundle args = getArguments();
Student_JSON = args.getString("STUDENT_JSON");
try {
examArray = new JSONArray(Student_JSON);
for (int i = 0; i < examArray.length(); i++) {
if (examArray.getJSONObject(i).getString("examTaken").equals("False")) {
exams = new ExamObject();
exams.setId(examArray.getJSONObject(i).getString("examID"));
exams.setName(examArray.getJSONObject(i).getString("examName"));
}
list.add(exams);
}
layout = (LinearLayout) view;
listview = (ListView) view.findViewById(R.id.listView1);
listview.setAdapter(new CurrentExamAdapter(list,
getActivity().getBaseContext()));
registerForContextMenu(listview);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} catch (JSONException e) {
Log.w("JSON PARSING ERROR",
"Imported JSON String can't be converted to JSON object");
e.printStackTrace();
}
return view;
}
...
}
NullPointerException指向以下类CurrentExamAdapter
CurrentExamAdapter
[package]
[imports]
public class CurrentExamAdapter extends BaseAdapter {
private ArrayList<ExamObject> data;
Context c;
public CurrentExamAdapter(ArrayList<ExamObject> datain, Context cin) {
data = datain;
c = cin;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_current_exams, null);
}
TextView examName = (TextView) v.findViewById(R.id.EXAMNAME);
TextView examStatus = (TextView) v.findViewById(R.id.status);
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME); //<---LOGCAT says this is
// where the exception occurs
examStatus.setText(msg.EXAM_STATUS);
return v;
}
}
除了不同的条件外,第一个标签片段与第二个标签片段完全相同。什么是试图引用的应用程序不存在?
修改 根据要求,这是堆栈跟踪:
10-26 04:28:41.529: W/IInputConnectionWrapper(21182): showStatusIcon on inactive InputConnection
10-26 04:28:44.856: I/Adreno-EGL(22413): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.040_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
10-26 04:28:44.856: I/Adreno-EGL(22413): OpenGL ES Shader Compiler Version: 17.01.11.SPL
10-26 04:28:44.856: I/Adreno-EGL(22413): Build Date: 02/26/14 Wed
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Branch:
10-26 04:28:44.856: I/Adreno-EGL(22413): Remote Branch: quic/kk_2.7_rb1.29
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Patches: NONE
10-26 04:28:44.856: I/Adreno-EGL(22413): Reconstruct Branch: NOTHING
10-26 04:28:44.903: D/OpenGLRenderer(22413): Enabling debug mode 0
10-26 04:28:53.881: W/InputEventReceiver(22413): Attempted to finish an input event but the input event receiver has already been disposed.
10-26 04:28:59.566: D/AndroidRuntime(22413): Shutting down VM
10-26 04:28:59.566: W/dalvikvm(22413): threadid=1: thread exiting with uncaught exception (group=0x416dad40)
10-26 04:28:59.577: E/AndroidRuntime(22413): FATAL EXCEPTION: main
10-26 04:28:59.577: E/AndroidRuntime(22413): Process: com.example.cs490project, PID: 22413
10-26 04:28:59.577: E/AndroidRuntime(22413): java.lang.NullPointerException
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.example.cs490project.CurrentExamAdapter.getView(CurrentExamAdapter.java:47)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.onMeasure(ListView.java:1175)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doFrame(Choreographer.java:544)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.handleCallback(Handler.java:733)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.dispatchMessage(Handler.java:95)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Looper.loop(Looper.java:136)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invoke(Method.java:515)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-26 04:28:59.577: E/AndroidRuntime(22413): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
空行指针表示不断出现在这一行:
ExamObject msg = data.get(position);
如果将该段代码转换为:
if(data.get(position) != null){
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME);
examStatus.setText(msg.EXAM_STATUS);
}
第二个选项卡将显示应该显示的内容。奇怪,但我不是在抱怨。