android - Handle orientation changes -


i've been developing project class , have reached point after searching in google still cannot find solution.

this error get

06-04 15:07:55.398    3480-3480/com.example.chema.agenda e/androidruntime﹕ fatal exception: main process: com.example.chema.agenda, pid: 3480 java.lang.nullpointerexception         @ com.example.chema.agenda.mainactivity_detallefragment.onactivitycreated(mainactivity_detallefragment.java:50)         @ android.support.v4.app.fragment.performactivitycreated(fragment.java:1797)         @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:979)         @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1138)         @ android.support.v4.app.backstackrecord.run(backstackrecord.java:740)         @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1501)         @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:458)         @ android.os.handler.handlecallback(handler.java:808)         @ android.os.handler.dispatchmessage(handler.java:103)         @ android.os.looper.loop(looper.java:193)         @ android.app.activitythread.main(activitythread.java:5296)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:515)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640)         @ dalvik.system.nativestart.main(native method) 

this class error when im rotating landscape portrait.

public class mainactivity_detallefragment extends fragment {  public mainactivity_detallefragment() { }   public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     // retain fragment  } @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {      viewgroup root = (viewgroup) inflater             .inflate(r.layout.fragment_detalle, null);       return root;  } public void onactivitycreated(bundle savedinstancestate){     super.onactivitycreated(savedinstancestate);     textview campotexto = (textview)getactivity().findviewbyid(r.id.textviewnombre);     textview campotexto2 = (textview)getactivity().findviewbyid(r.id.textview8);     imageview im = (imageview)getactivity().findviewbyid(r.id.imageview);     im.setimagebitmap(mainactivity.getcontacto().getimagen());     campotexto.settext(mainactivity.getcontacto().getname() +" "+  mainactivity.getcontacto().getapellidos());     campotexto2.settext(mainactivity.getcontacto().getphonenumber());   } 

}

exactly in lane im.setimagebitmap(mainactivity.getcontacto().getimagen());

and main activity

public class mainactivity extends actionbaractivity implements fragmentlistar.onitemclickedlistener{ private drawerlayout desplegable; private actionbardrawertoggle toggle; integer[] imgid = {         r.mipmap.create_contact,         r.mipmap.delete_action_bar,         r.mipmap.edit_contact }; public listview opciones; private boolean dosfragmentos; static contact contacto;  @override protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       string[] valores = getresources().getstringarray(r.array.despl);     customlistadapter adapter = new customlistadapter(this, valores, imgid);     desplegable = (drawerlayout) findviewbyid(r.id.drawer_layout);     opciones = (listview) findviewbyid(r.id.left_drawer);     opciones.setadapter(adapter);      toggle = new actionbardrawertoggle(this, desplegable, r.mipmap.white_background,             r.string.drawer_open, r.string.drawer_close);     getsupportactionbar().setdisplayhomeasupenabled(true);     getsupportactionbar().sethomebuttonenabled(true);      fragmenttransaction tx = getsupportfragmentmanager().begintransaction();     tx.replace(r.id.content_frame, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.fragmentlistar"));     tx.commit();      opciones.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             textview c = (textview) view.findviewbyid(r.id.item);             string item = c.gettext().tostring();             if (item.equals("crear")) {                 desplegable.setdrawerlistener(new drawerlayout.simpledrawerlistener() {                     @override                     public void ondrawerclosed(view drawerview) {                         super.ondrawerclosed(drawerview);                         if(getresources().getconfiguration().orientation== configuration.orientation_landscape){                             fragmentmanager fragmgr = getsupportfragmentmanager();                             fragment currentfragment = fragmgr.findfragmentbyid(r.id.content_frame2);                             fragmenttransaction fragtrans = fragmgr.begintransaction();                             fragtrans.remove(currentfragment);                              fragmenttransaction tx = getsupportfragmentmanager().begintransaction();                             tx.replace(r.id.content_frame_landscape, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.countfragment"));                             tx.commit();                         }else {                             fragmenttransaction tx = getsupportfragmentmanager().begintransaction();                             tx.replace(r.id.content_frame, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.countfragment"));                             tx.commit();                         }                     }                 });                 desplegable.closedrawer(opciones);             }             if (item.equals("borrar")) {                 desplegable.setdrawerlistener(new drawerlayout.simpledrawerlistener() {                     @override                     public void ondrawerclosed(view drawerview) {                         super.ondrawerclosed(drawerview);                         fragmenttransaction tx = getsupportfragmentmanager().begintransaction();                         tx.replace(r.id.content_frame, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.fragmentdelete"));                         tx.commit();                     }                 });                 desplegable.closedrawer(opciones);             }         }     });    }  public void onbackpressed() {     log.d("cda", "onbackpressed called");     intent setintent = new intent(this, mainactivity.class);      startactivity(setintent); }  public void onitemclicked(int id) {     databasehandler d1 = new databasehandler(this.getapplicationcontext());     contacto = d1.getcontact(id);       fragmenttransaction tx = getsupportfragmentmanager().begintransaction();     if(getresources().getconfiguration().orientation == configuration.orientation_landscape){         tx.replace(r.id.content_frame2, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));         tx.commit();     }      else {         if ((getresources().getconfiguration().screenlayout &                 configuration.screenlayout_size_mask) ==                 configuration.screenlayout_size_large) {             tx.replace(r.id.content_frame2, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));             tx.commit();          } else {             tx.replace(r.id.content_frame, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));             tx.commit();         }     }  }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }    @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();     if (toggle.onoptionsitemselected(item)) {      }     //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } protected void onpostcreate(bundle savedinstancestate) {     super.onpostcreate(savedinstancestate);     toggle.syncstate(); }  public void onconfigurationchanged(configuration newconfig) {     super.onconfigurationchanged(newconfig);     toggle.onconfigurationchanged(newconfig);       // checks orientation of screen  } static public contact getcontacto(){     return contacto; } 

and fragmentlistar class:

public class fragmentlistar extends listfragment {  private list<contact> c1; onitemclickedlistener mlistener = sdummycallbacks;  private static onitemclickedlistener sdummycallbacks = new onitemclickedlistener() {     @override     public void onitemclicked(int id) {     } };  public fragmentlistar() {     c1 = null; }   public interface onitemclickedlistener {     public void onitemclicked(int id); }  public void onattach(activity activity) {     super.onattach(activity);     try {         mlistener = (onitemclickedlistener) activity;     } catch (classcastexception e) {         throw new classcastexception(activity.tostring() + " must implement onitemclickedlistener");     } }  @override public void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);      databasehandler d1 = new databasehandler(getactivity().getapplicationcontext());     this.c1 = d1.getallcontacts();      d1.close();      // estable cemos el adapter la lista del fragment     setlistadapter(new contactlistadapter(getactivity(),             c1)); }  @override public void onlistitemclick(listview l, view v, int position, long id) {     // todo auto-generated method stub     super.onlistitemclick(l, v, position, id);      // mostramos un mensaje con el elemento pulsado       mlistener.onitemclicked(c1.get(position).getid());  } 

i don't know why mainactivity_detallefragment called once mainactivity restarts on rotation. should fragmentlistar 1 called.

fixing contacto non-static

public void onitemclicked(int id) {      bundle bundle = new bundle();     bundle.putstring("id", integer.tostring(id));     // set fragmentclass arguments     mainactivity_detallefragment fragobj = new mainactivity_detallefragment();     fragobj.setarguments(bundle);      fragmenttransaction tx = getsupportfragmentmanager().begintransaction();     if(getresources().getconfiguration().orientation == configuration.orientation_landscape){         tx.replace(r.id.content_frame2, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));         tx.commit();     }      else {         if ((getresources().getconfiguration().screenlayout &                 configuration.screenlayout_size_mask) ==                 configuration.screenlayout_size_large) {             tx.replace(r.id.content_frame2, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));             tx.commit();          } else {             tx.replace(r.id.content_frame, fragment.instantiate(mainactivity.this, "com.example.chema.agenda.mainactivity_detallefragment"));             tx.commit();         }     }  } 

never ever use static methods retrieve information activity!

what happens here contacto populated once click item in list. variable , whatever points not retained across screen orientation meaning after rotate screen null, hence nullpointerexception.

there's couple of things wrong code, question though npe i'll focus on 1 here.

the quick'n'dirty way fix check whether getcontacto() returns null or not. make mainactivity.getcontacto() non-static method (getactivity().getcontacto()).

what need dig in though why detail fragment retained @ layout should not contain detail fragment list fragment, right?

what need consider using loader mentioned in comment question. loader results retained across orientation changes , you'd not run issue @ all. it's more efficient if don't have reload after each orientation change.

also consider why need retrieve contact in activity. far can tell code need in detail fragment why not move code fragment?

databasehandler d1 = new databasehandler(this.getapplicationcontext()); contacto = d1.getcontact(id); 

of course fragment needs know id that's thing since that's information detail fragment should have.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -