简体   繁体   English

NullPointerException textView.setText()

[英]NullPointerException textView.setText()

i'am here because i cant figured out why i'am getting a nullPointerException with no valid reason, i also do a try and catch block to help me figure the issue but the message is null and the printStackTrace is (void), this issue is occuring in a array loop, in the first item it's everything allright but in the second it's throws NullPointerExcption. 我在这里是因为我无法弄清楚为什么没有正当理由就得到nullPointerException,我也做了try and catch块来帮助我解决问题,但是消息为null,而printStackTrace为(void),此问题是在数组循环中发生的,在第一个项目中一切正常,但是在第二个项目中抛出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

Debug Expressions 调试表达式

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

        }
    }

}

Try running this code outside of the onCreate method. 尝试在onCreate方法之外运行此代码。 The log says 'Unable to start activity' this happens when something in the onCreate method throws an exception. 日志显示“无法启动活动”,这是在onCreate方法中的某些事件引发异常时发生的。 This also could be a reason this code fails, textviews aren't initialized yet. 这也可能是此代码失败,文本视图尚未初始化的原因。

Try running this code in the onResume method. 尝试在onResume方法中运行此代码。

It's solved was several structure problems, because i was doing this without checking null array's and other several mistakes, this is the result of developing when we are in a rush. 解决了几个结构问题,因为我这样做时没有检查null数组和其他几个错误,这是我们匆忙开发时的结果。 Thanks all for the time spent ;) PS: Can the admin's delete this, this question will not help anybody 感谢所有的时间;)PS:管理员可以删除它,这个问题对任何人都没有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM