java - Getting error when changing other activity's TextView -


i'm beginner @ android , java , have error when trying change other activity's textview. i've created mainactivity, , 1 (playernames).

i've set playernames launcher activity, maybe error that, don't know. post error , code:

fatal exception: main  process: com.example.android.mapamundi, pid: 1423 java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.android.mapamundi/com.example.android.mapamundi.playernames}: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2236) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference @ android.support.v7.app.appcompatdelegateimplbase.<init>(appcompatdelegateimplbase.java:68) @ android.support.v7.app.appcompatdelegateimplv7.<init>(appcompatdelegateimplv7.java:146) @ android.support.v7.app.appcompatdelegateimplv11.<init>(appcompatdelegateimplv11.java:28) @ android.support.v7.app.appcompatdelegateimplv14.<init>(appcompatdelegateimplv14.java:41) @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:190) @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:172) @ android.support.v7.app.appcompatactivity.getdelegate(appcompatactivity.java:512) @ android.support.v7.app.appcompatactivity.findviewbyid(appcompatactivity.java:184) @ com.example.android.mapamundi.playernames.<init>(playernames.java:12) @ java.lang.reflect.constructor.newinstance(native method) @ java.lang.class.newinstance(class.java:1606) @ android.app.instrumentation.newactivity(instrumentation.java:1066) @ android.app.activitythread.performlaunchactivity(activitythread.java:2226) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387)  @ android.app.activitythread.access$800(activitythread.java:151)  @ android.app.activitythread$h.handlemessage(activitythread.java:1303)  @ android.os.handler.dispatchmessage(handler.java:102)  @ android.os.looper.loop(looper.java:135)  @ android.app.activitythread.main(activitythread.java:5254)  @ java.lang.reflect.method.invoke(native method)  @ java.lang.reflect.method.invoke(method.java:372)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903)  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 

mainactivity.java

well, on mainactivity cast textviews public static textview tv1, don't post it

playernames.java

public class playernames extends appcompatactivity implements view.onclicklistener {  button accept = (button) findviewbyid(r.id.changenames);    edittext playerinput1 = (edittext) findviewbyid(r.id.playernameinput1); edittext playerinput2 = (edittext) findviewbyid(r.id.playernameinput2); edittext playerinput3 = (edittext) findviewbyid(r.id.playernameinput3); edittext playerinput4 = (edittext) findviewbyid(r.id.playernameinput4);   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_player_names);        accept.setonclicklistener(this); }  @override public void onclick(view view) {     switch (view.getid()){         case (r.id.changenames):              mainactivity.playername1.settext(playerinput1.tostring());             mainactivity.playername2.settext(playerinput2.tostring());             mainactivity.playername3.settext(playerinput3.tostring());             mainactivity.playername4.settext(playerinput4.tostring());              intent intent = new intent(playernames.this, mainactivity.class);              startactivity(intent);     } } 

}

i post androidmanifest.xml, in case have error or causes error

<activity         android:name=".mainactivity"         android:label="@string/app_name"         android:theme="@style/apptheme.noactionbar">         <intent-filter>             <action android:name="com.example.android.mapamundi.mainactivity" />              <category android:name="android.intent.category.default" />         </intent-filter>     </activity>     <activity android:name=".playernames">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity> 

can please tell me doing wrong? tks!!

you calling findviewbyid() early, before activity created. cannot in field initialiser. has in (or after) oncreate(). , after setcontentview(), because first have set content of activity, before can try find views inside of it.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -