java - Runtime Exception in Dynamic Fragments -


i trying create activity load different fragments dynamically. how main activity like

import android.os.bundle; import android.app.activity; import android.app.fragment; import android.app.fragmenttransaction;     public class user_details extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_fragment_loader);         fragment fragment=new mail_fragment();          fragmenttransaction fragmenttransaction =               getfragmentmanager().begintransaction();           fragmenttransaction.add(r.id.fragment_place, fragment);           fragmenttransaction.commit();  } 

i getting following error during run time. please check error here

the fragment this

import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;     public class mail_fragment extends fragment { @override public view oncreateview(layoutinflater inflater,                          viewgroup container, bundle savedinstancestate) {     return inflater.inflate(r.layout.fragment_mail, container, false); }} 

the xml file of main activity(user_details) activity_fragment_loader

<?xml version="1.0" encoding="utf-8"?>  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="ask me later"     android:id="@+id/but_skip"     android:layout_alignparentbottom="true"     android:layout_centerhorizontal="true" /> <fragment     android:id="@+id/fragment_place"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_above="@+id/but_skip"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" />     </relativelayout> 

the xml file of mail_fragment activity is

<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent" android:layout_height="match_parent">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textappearance="?android:attr/textappearancemedium"     android:text="enter e-mail id"     android:id="@+id/mail"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_margintop="119dp"     android:textcolor="#000000"     android:layout_gravity="center_horizontal|top" />  <edittext     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:inputtype="textemailaddress"     android:ems="10"     android:id="@+id/edittext"     android:layout_centervertical="true"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:layout_alignparentright="true"     android:layout_alignparentend="true"     android:layout_gravity="center" />  </framelayout> 

i have referred similar questions. of them suggest place setcontentview() in user_details activity after committing fragmenttracsaction. have tried still getting same error.

i add statically embeded fragment in xml file , working perfectly. when pragmatically throwing me error.

any appreciated

the fragmenttransaction.add method takes id of viewgroup adding fragment, not id of fragment. have 2 alternates, either specify name attribute of fragment in xml like

<fragment android:name="your_package_name.mail_fragment" android:id="@+id/fragment_place" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/but_skip" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> 

and in java, instead of calling

fragment fragment = new mail_fragment(); 

use

fragment fragment = (fragment)findviewbyid(r.id.fragment_place); 

and remaining usual.

the other alternate not add fragment xml of activity, create in java. in case, have specify viewgroup id in fragmenttransaction.add() method. hope helps.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -