android - Why is it showing that TextView object is null? -


i making app in in using fragments. have made in portrait in landscape mode.

in portrait mode have done listview shown , on selecting item on next activity description shown

in landscape mode have used 2 fragments in same activity in 1 fragment has listview , second fragment has description

but when clicking item in list view showing me error

cannot invoke method on null textview object  

but have initialised n onactivitycreated() method in fragment2.java why giving error?

main.xml (portrait)

<linearlayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.grepthor.fragment4.mainactivity">      <fragment         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/fragment1"         android:name="com.grepthor.fragment4.fragment1"         />  </linearlayout> 

main.xml (landscape)

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal" android:layout_width="match_parent"     android:layout_height="match_parent">       <fragment         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1"         android:id="@+id/fragment1"         android:name="com.grepthor.fragment4.fragment1"/>       <fragment         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1"         android:id="@+id/fragment2"         android:name="com.grepthor.fragment4.fragment2"/> </linearlayout> 

main.java

import android.app.fragmentmanager; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle;  public class mainactivity extends appcompatactivity implements fragment1.communicator {       fragment1 f1;     fragment2 f2;     fragmentmanager fm;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          fm=getfragmentmanager();         f1= (fragment1) fm.findfragmentbyid(r.id.fragment1);         f1.setcommunicator(this);     }      @override     public void respond(int position) {         f2= (fragment2) fm.findfragmentbyid(r.id.fragment2);          if(f2!=null && f2.isvisible())         { f2.changedata(position);         }         else         {             intent i=new intent(this,secondactivity.class);             i.putextra("index",position);             startactivity(i);         }     } } 

fragment1.xml

<relativelayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.grepthor.fragment4.fragment1">      <listview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/list"/>  </relativelayout> 

fragment1.java

import android.app.fragment; import android.support.annotation.nullable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast;  public class fragment1 extends fragment implements adapterview.onitemclicklistener {      listview l;     communicator comm;     @nullable     @override     public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) {         return inflater.inflate(r.layout.activity_fragment1,container,false);     }       @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);            l= (listview) getactivity().findviewbyid(r.id.list);         arrayadapter ad=arrayadapter.createfromresource(getactivity(),r.array.topic,android.r.layout.simple_list_item_1);         l.setadapter(ad);        // toast.maketext(getactivity(),"helo",toast.length_long).show();         l.setonitemclicklistener(this);     }      @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) { comm.respond(position);     }       public void setcommunicator(communicator c)     {         comm=c;     }      interface communicator     {         void respond(int data);     } } 

fragment2.xml

<relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.grepthor.fragment4.fragment2">        <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/text"/>      </relativelayout> 

fragment2.java

public class fragment2 extends fragment {      textview t;      @nullable     @override     public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) {         return inflater.inflate(r.layout.activity_fragment2,container,false);     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         // toast.maketext(getactivity(),"helo",toast.length_long).show();         t=(textview)getactivity().findviewbyid(r.id.text);     }       public void changedata(int position)     {         string des[]= getresources().getstringarray(r.array.des);         t.settext(des[position]);     } } 

secondactivity.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.grepthor.fragment4.secondactivity">       <fragment         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/fragment2"         android:name="com.grepthor.fragment4.fragment2"/> </linearlayout> 

secondactivity.java

import android.content.intent; import android.app.fragmentmanager; import android.support.v7.app.appcompatactivity; import android.os.bundle;  public class secondactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_second);          intent i=getintent();         int position=i.getextras().getint("index",0);          fragmentmanager fm=getfragmentmanager();         fragment2 f2= (fragment2) fm.findfragmentbyid(r.id.fragment2);      f2.changedata(position);        } } 

strings.xml

<resources>     <string name="app_name">fragment4</string>      <string-array name="topic">         <item>heading</item>         <item>stories</item>         <item>settings</item>         <item>music</item>     </string-array>      <string-array name="des">         <item>ldwkflkwdflkdqdvmqmdvmqdvmqlkrvqkneroqknre;qoiengqrknqrqvqfvaa</item>         <item>vqdlkvnqldvnqdnvqdnvqepojpqojjoergjelgefbfmsmfv</item>         <item>dwkfmnqdkwnfqkjwdnflwndfqkwheflqierqieurpqeirqrkmqwrgfqnqnmdqd</item>         <item>skjsbdkafsknaslnaslfnaljfhadfkjgjhgasjdvsadablvasfgasfkgnjlkrglrgqmqbvmadbsv</item>     </string-array>  </resources> 

logcat

fatal exception: main                                                                         process: com.grepthor.fragment4, pid: 31014                                                                         java.lang.runtimeexception: unable start activity componentinfo{com.grepthor.fragment4/com.grepthor.fragment4.secondactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference                                                                             @ android.app.activitythread.performlaunchactivity(activitythread.java:2430)                                                                             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2490)                                                                             @ android.app.activitythread.access$900(activitythread.java:157)                                                                             @ android.app.activitythread$h.handlemessage(activitythread.java:1351)                                                                             @ android.os.handler.dispatchmessage(handler.java:102)                                                                             @ android.os.looper.loop(looper.java:148)                                                                             @ android.app.activitythread.main(activitythread.java:5437)                                                                             @ java.lang.reflect.method.invoke(native method)                                                                             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)                                                                             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)                                                                          caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference                                                                             @ com.grepthor.fragment4.fragment2.changedata(fragment2.java:35)                                                                             @ com.grepthor.fragment4.secondactivity.oncreate(secondactivity.java:21)                                                                             @ android.app.activity.performcreate(activity.java:6532)                                                                             @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1108)                                                                             @ android.app.activitythread.performlaunchactivity(activitythread.java:2383)                                                                             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2490)                                                                              @ android.app.activitythread.access$900(activitythread.java:157)                                                                              @ android.app.activitythread$h.handlemessage(activitythread.java:1351)                                                                              @ android.os.handler.dispatchmessage(handler.java:102)                                                                              @ android.os.looper.loop(looper.java:148)                                                                              @ android.app.activitythread.main(activitythread.java:5437)                                                                              @ java.lang.reflect.method.invoke(native method)                                                                              @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)                                                                              @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)  

your fragment2 has textview , you're trying find in activity. need instead in fragment2 , remove onactivitycreated() :

@override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.activity_fragment2,container,false);     textview textview = (textview) rootview.findviewbyid(r.id.text); } 

also in future, post relevant parts of code vs posting of it.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -