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);
}
}
}
答案 0 :(得分:0)
尝试在onCreate方法之外运行此代码。日志显示“无法启动活动”当onCreate方法中的某些内容抛出异常时会发生这种情况。这也可能是此代码失败的原因,textviews尚未初始化。
尝试在onResume方法中运行此代码。
答案 1 :(得分:0)
它解决了几个结构问题,因为我这样做而没有检查空数组和其他几个错误,这是我们匆忙时开发的结果。谢谢所有花费的时间;)PS:管理员可以删除这个,这个问题对任何人都没有帮助