在设备上运行时崩溃

时间:2018-08-04 05:27:34

标签: java android nullpointerexception

我是Android编程的新手 我尝试学习它并在git * ub网站上获取示例应用程序示例,我试图解决该应用程序中的一些错误,并且在编译该应用程序后能够运行,但是当我从应用程序帖子,错误是这样的

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.newsagni/com.app.newsagni.ActivityPostDetails}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2490)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2550)
    at android.app.ActivityThread.access$1100(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5615)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.app.newsagni.ActivityPostDetails.displayPostData(ActivityPostDetails.java:190)
    at com.app.newsagni.ActivityPostDetails.onCreate(ActivityPostDetails.java:98)
    at android.app.Activity.performCreate(Activity.java:6362)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2443)
    ... 9 more

以下列表来自

ActivityPostDetails.java https://pastebin.com/h56LUx2T

activity_post_details.xml https://pastebin.com/hwYpza37

您能帮我克服这个错误吗

1 个答案:

答案 0 :(得分:0)

您没有ID为“ date”的TextView,因此Android无法找到TextView。因此,在尝试使用NPE时会得到它。将TextView添加到您的活动中,您应该会很好。