I cannot figure out why I keep getting errors. The errors keep telling me that my date is null
and that I am setting it to null
, but I'm not. I also have looked online and I have literally pasted the code in exactly and followed all the directions and still no luck.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
TextView date = (TextView) findViewById(R.id.textView);
String Date= DateFormat.getDateTimeInstance().format(new Date());
date.setText(Date);
}
Here is the XML file for the schedule_layout
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DATE HERE"
android:id="@+id/date_text"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
And here is my LogCat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nick.veronaremindersapp/com.example.nick.veronaremindersapp.MainActivity}: 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:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
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.example.nick.veronaremindersapp.MainActivity.onCreate(MainActivity.java:61)
You are not using the correct id when you call findViewById
. Here you use textView
--
TextView date = (TextView) findViewById(R.id.textView);
but here you use date_text
android:id="@+id/date_text"
Change one or the other so both ids are the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.