java - Why does this cause a NullPointerException? -
i have program follows, can tell me reason getting npe.
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); secondclass x = new secondclass(); x.stext("test spring"); } } public class secondclass extends mainactivity { public void stext(string s) { textview text = (textview) findviewbyid(r.id.text); text.settext(s); } } mainfest
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ... <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/text" /> </relativelayout> logcat
08-16 01:31:11.320 15032-15032/? e/trace: error opening trace file: no such file or directory (2) 08-16 01:31:11.420 836-987/? e/embeddedlogger: app crashed! process: com.example.myapplication 08-16 01:31:11.420 836-987/? e/embeddedlogger: app crashed! package: com.example.myapplication v1 (1.0) 08-16 01:31:11.420 836-987/? e/embeddedlogger: application label: application 08-16 01:31:11.420 15032-15032/? e/androidruntime: fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.example.myapplication/com.example.myapplication.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java) @ android.app.activitythread.handlelaunchactivity(activitythread.java) @ android.app.activitythread.access$600(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java) @ android.os.handler.dispatchmessage(handler.java) @ android.os.looper.loop(looper.java) @ android.app.activitythread.main(activitythread.java) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java) @ com.android.internal.os.zygoteinit.main(zygoteinit.java) @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:110) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ android.app.activity.findviewbyid(activity.java) @ com.example.myapplication.secondclass.stext(secondclass.java:13) @ com.example.myapplication.mainactivity.oncreate(mainactivity.java:14) @ android.app.activity.performcreate(activity.java) @ android.app.instrumentation.callactivityoncreate(instrumentation.java) to understanding, should instantiate , initialize secondclass object call stext() method. stext method should store reference in textview. next calls settext() method on textview, changes text in textview passed stext().
the logcat says throws nullpointerexception i'm not entirely sure why. if rookie mistake, it's because rookie. thank you.
you need override oncreate in secondactivity , have setcontentview(r.layout.mylayout). can initialize textview.
also need start activity using intent
intent intent = new intent(mainactivtiy.this,secondactivity.class); startactivity(intent); remember make entry activity (secondactivtiy) in manifest file
if need pass string second activity use intent.putextra
intent intent = new intent(mainactivtiy.this,secondactivity.class); intent.putextra("key",mystring); startactivity(intent); then in secondactivity
textview tv ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mysecondlayout); tv = (textview) findviewbyid(r.id.textview1); bundle extras = getintent().getextras(); if (extras != null) { string value = extras.getstring("key"); tv.settext(value); //get value based on key } http://developer.android.com/reference/android/content/intent.html#putextra(java.lang.string, android.os.bundle)
you can findviewbyid of current view herarchy set activity.
also need startactivity using intent rather doing econdclass x = new secondclass()
Comments
Post a Comment