我在这里是因为我不知道为什么我会在没有正当理由的情况下得到一个 nullPointerException,我还做了一个 try and catch 块来帮助我解决这个问题,但是消息为空并且 printStackTrace 是(无效),这个问题发生在数组循环中,在第一项中一切正常,但在第二项中抛出 NullPointerExcption。
09-16 14:45:09.053: E/AndroidRuntime(4332): FATAL EXCEPTION: main
09-16 14:45:09.053: E/AndroidRuntime(4332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moody/activities.MainActivity}: java.lang.NullPointerException 09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread.access$600(ActivityThread.java:123) 09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.os.Looper.loop(Looper.java:137) 09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-16 14:45:09.053: E/AndroidRuntime(4332): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 14:45:09.053: E/AndroidRuntime(4332): at java.lang.reflect.Method.invoke(Method.java:511)
09-16 14:45:09.053: E/AndroidRuntime(4332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-16 14:45:09.053: E/AndroidRuntime(4332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-16 14:45:09.053: E/AndroidRuntime(4332): at dalvik.system.NativeStart.main(Native Method)
09-16 14:45:09.053: E/AndroidRuntime(4332): Caused by: java.lang.NullPointerException
09-16 14:45:09.053: E/AndroidRuntime(4332): at fragments.TopicsPreview.createTopicsContent(TopicsPreview.java:162)
09-16 14:45:09.053: E/AndroidRuntime(4332): at fragments.TopicsPreview.createTopicsRows(TopicsPreview.java:85)
09-16 14:45:09.053: E/AndroidRuntime(4332): at fragments.TopicsPreview.onCreateView(TopicsPreview.java:59)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.BackStackRecord.run(BackStackRecord.java:622)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.Activity.performStart(Activity.java:4474)
09-16 14:45:09.053: E/AndroidRuntime(4332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
09-16 14:45:09.053: E/AndroidRuntime(4332): ... 11 more
protected void createTopicsContent(MoodleCourseContent[] courseContent,
LayoutInflater inflater, LinearLayout insertPoint, String courseId) {
for (int j = 0; j < courseContent.length; j++) {
final LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
View topicsView = inflater.inflate(R.layout.topics_preview_context,
null);
TextView topicName = (TextView) topicsView
.findViewById(R.id.topic_label);
topicName.setText(courseContent[j].getName());
// Loop for the modules array
MoodleModule[] modules = courseContent[j].getMoodleModules();
for (int i = 0; i < modules.length; i++) {
final TextView topicModule = (TextView) topicsView
.findViewById(R.id.content_preview_textView);
if (!modules[i].getName().isEmpty()) {
try {
topicModule.setText(modules[i].getName());
} catch (NullPointerException e) {
e.printStackTrace();
e.getMessage();
}
}
// Where the textview id will be course id and the textview
// tag will be the topic id
topicModule.setId(Integer.parseInt(courseId));
topicModule.setTag(Long.toString(modules[i].getId()));
row.addView(topicsView);
insertPoint.addView(row);
}
}
}