java - Can't get fragment to show, no view found for id -


i trying fragment show contains edittext , button. new using fragments, not sure error message when trying create fragment means.

i have class extends fragment, edittext , button created.

public class editnamefragment extends android.support.v4.app.fragment {       edittext edittext;     imagebutton button;      public editnamefragment(){      }       @override     public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {          view view = inflater.inflate(r.layout.edit_name_dialog, container, false);          edittext = (edittext) view.findviewbyid(r.id.edittextdialog);         button = (imagebutton) view.findviewbyid(r.id.submitnewitembuttondialog);          button.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 //stuff             }         });          return view;      } 

here edit_name_dialog.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal" android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_gravity="center"     android:id="@+id/edit_name_dialog"     >      <edittext         android:id="@+id/edittextdialog"         android:layout_width="wrap_content"         android:layout_height="wrap_content"      />      <imagebutton         android:id="@+id/submitnewitembuttondialog"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"          />   </linearlayout> 

and here in main activity (which must extend fragmentactivity because of part) try set fragment. think has id referencing. i have seen people using container classes when using fragments, not understand why done.

 fragmentmanager fragmentmanager = getsupportfragmentmanager();  fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();   editnamefragment fragment = new editnamefragment();  fragmenttransaction.add(r.id.edit_name_dialog, fragment, "tag");    fragmenttransaction.commit(); 

i error message when trying run code above

no view found id 0x7f09002a (com.myapp:id/edit_name_dialog) fragment editnamefragment 

if explain missing here/ why people use container classes, great. know people add fragments using xml, using java.

edit

i have added class extends fragmentactivity, following model container class

public class editnamefragmentactivity extends fragmentactivity {     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.edit_name_fragment_container);     } } 

is parameter setcontentview supposed layout, or id? here xml file defines fragment should be

edit_name_fragment_container.xml

<linearlayout 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"     >      <fragment android:name="com.returnjump.spoilfoil.editnamefragment"         android:id="@+id/edit_name_fragment_container"         android:layout_weight="1"         android:layout_width="match_parent"         android:layout_height="match_parent"         tools:layout="@layout/edit_name_fragment" />  </linearlayout> 

so parameter in

fragmenttransaction.add(r.id.edit_name_dialog, fragment, "tag"); 

this supposed reference id of fragment, correct?

it still gives me same error, missing?

there 2 ways add fragment activity documentation say:

  • "statically": declaring fragment inside activity's layout file.
  • "dynamically": adding fragment programmatically. tried do.

here documentation: http://developer.android.com/guide/components/fragments.html

if wish add dynamically, here documentation part want read:

at time while activity running, can add fragments activity layout. need specify viewgroup in place fragment. make fragment transactions in activity (such add, remove, or replace fragment), must use apis fragmenttransaction. can instance of fragmenttransaction activity this:

fragmentmanager fragmentmanager = getfragmentmanager() fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); 

you can add fragment using add() method, specifying fragment add , view in insert it. example:

examplefragment fragment = new examplefragment(); fragmenttransaction.add(r.id.fragment_container, fragment); fragmenttransaction.commit(); 

the first argument passed add() viewgroup in fragment should placed, specified resource id, , second parameter fragment add. once you've made changes fragmenttransaction, must call commit() changes take effect.

and why use dynamic fragments instead of static fragments, has been made interactive ui allowing handle different fragments 1 activity please.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -