java - I just can't get a fragment to work -


i trying add simple fragment , doing wrong. create blank "mainactivity" in android studio, add fragment , try wire up:

mainactivity.java:

package com.fragtester.fragtester.fragtester;  import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem;   public class mainactivity extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // check activity using layout version         // fragment_container framelayout         if (findviewbyid(r.id.fragment_container) != null) {              // however, if we're being restored previous state,             // don't need , should return or else             // end overlapping fragments.             if (savedinstancestate != null) {                 return;             }              // create new fragment placed in activity layout             blankfragment firstfragment = new blankfragment();              // in case activity started special instructions             // intent, pass intent's extras fragment arguments             firstfragment.setarguments(getintent().getextras());              // add fragment 'fragment_container' framelayout             getsupportfragmentmanager().begintransaction()                     .add(r.id.fragment_container, firstfragment).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();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">      <framelayout xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/fragment_container"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </relativelayout> 

blankfragment.java

package com.fragtester.fragtester.fragtester;  import android.app.activity; import android.net.uri; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;   /**  * simple {@link fragment} subclass.  * activities contain fragment must implement  * {@link blankfragment.onfragmentinteractionlistener} interface  * handle interaction events.  * use {@link blankfragment#newinstance} factory method  * create instance of fragment.  */ public class blankfragment extends android.support.v4.app.fragment {     // todo: rename parameter arguments, choose names match     // fragment initialization parameters, e.g. arg_item_number     private static final string arg_param1 = "param1";     private static final string arg_param2 = "param2";      // todo: rename , change types of parameters     private string mparam1;     private string mparam2;      private onfragmentinteractionlistener mlistener;      /**      * use factory method create new instance of      * fragment using provided parameters.      *      * @param param1 parameter 1.      * @param param2 parameter 2.      * @return new instance of fragment blankfragment.      */     // todo: rename , change types , number of parameters     public static blankfragment newinstance(string param1, string param2) {         blankfragment fragment = new blankfragment();         bundle args = new bundle();         args.putstring(arg_param1, param1);         args.putstring(arg_param2, param2);         fragment.setarguments(args);         return fragment;     }      public blankfragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if (getarguments() != null) {             mparam1 = getarguments().getstring(arg_param1);             mparam2 = getarguments().getstring(arg_param2);         }     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         return inflater.inflate(r.layout.fragment_blank, container, false);     }      // todo: rename method, update argument , hook method ui event     public void onbuttonpressed(uri uri) {         if (mlistener != null) {             mlistener.onfragmentinteraction(uri);         }     }      @override     public void onattach(activity activity) {         super.onattach(activity);         try {             mlistener = (onfragmentinteractionlistener) activity;         } catch (classcastexception e) {             throw new classcastexception(activity.tostring()                     + " must implement onfragmentinteractionlistener");         }     }      @override     public void ondetach() {         super.ondetach();         mlistener = null;     }      /**      * interface must implemented activities contain      * fragment allow interaction in fragment communicated      * activity , potentially other fragments contained in      * activity.      * <p/>      * see android training lesson <a href=      * "http://developer.android.com/training/basics/fragments/communicating.html"      * >communicating other fragments</a> more information.      */     public interface onfragmentinteractionlistener {         // todo: update argument type , name         public void onfragmentinteraction(uri uri);     }  } 

fragment_blank.xml:

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.fragtester.fragtester.fragtester.blankfragment">      <!-- todo: update blank fragment layout -->     <textview android:layout_width="match_parent" android:layout_height="match_parent"         android:text="@string/hello_blank_fragment" />  </framelayout> 

what doing wrong?!?!? have done bunch of different tutorials , cannot working me. please help! frustrated.

are encountering errors? because looking @ code, can see you've created default fragment , automatically added callback interface fragment, mainactivity class not implement. i'm sure if looked @ logcat, see error message along lines of "mainactivity must implement onfragmentinteractionlistener".

in future, learn check logcat , post stacktrace if have one, it'll make getting answer easier.

to fix this, should remove callback interface fragment, i'm sure won't needing @ point. this, remove following lines of code blankfragment:

private onfragmentinteractionlistener mlistener;  // ...  // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) {     if (mlistener != null) {         mlistener.onfragmentinteraction(uri);     } }  @override public void onattach(activity activity) {     super.onattach(activity);     try {         mlistener = (onfragmentinteractionlistener) activity;     } catch (classcastexception e) {         throw new classcastexception(activity.tostring()                 + " must implement onfragmentinteractionlistener");     } }  @override public void ondetach() {     super.ondetach();     mlistener = null; }  /**  * interface must implemented activities contain  * fragment allow interaction in fragment communicated  * activity , potentially other fragments contained in  * activity.  * <p/>  * see android training lesson <a href=  * "http://developer.android.com/training/basics/fragments/communicating.html"  * >communicating other fragments</a> more information.  */ public interface onfragmentinteractionlistener {     // todo: update argument type , name     public void onfragmentinteraction(uri uri); } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -