0

我在这里是因为我不知道为什么我会在没有正当理由的情况下得到一个 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);

        }
    }

}
4

2 回答 2

0

它解决了几个结构问题,因为我这样做没有检查空数组和其他几个错误,这是我们赶时间开发的结果。感谢大家花费的时间;) PS:管理员可以删除这个吗,这个问题对任何人都没有帮助

于 2013-09-17T02:12:19.747 回答
0

尝试在 onCreate 方法之外运行此代码。当 onCreate 方法中的某些内容引发异常时,日志会显示“无法启动活动”。这也可能是此代码失败的原因,文本视图尚未初始化。

尝试在 onResume 方法中运行此代码。

于 2013-09-16T15:17:32.913 回答