android - Starting new fragment from mainactivity -
i read lot of questions here, didn't proper solution problem. have mainactivity java class, , have several activities in app. now, created "tabbed activity" automatically in android studio. have imagebutton use opening new fragment. new fragment called "generaldiscription". learned, fragment can't started activity, need use fragmentmanagener fragment open. app starts, when click on imagebutton, fragment should start, app crashes.
in following, can see mainactivity.java, , fragments java , xml. thank in anticipation.
mainactivity.java
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); final imagebutton generaldiscriptionbutton = (imagebutton) findviewbyid(r.id.imagebutton); setsupportactionbar(toolbar); checkfirstrun(); generaldiscriptionbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { fragmentmanager fm = getsupportfragmentmanager(); generaldiscription fragment = new generaldiscription(); fm.begintransaction().add(r.id.main_content, ); // don't know write here. } });
my activity_generaldiscription.xml created automatically
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.coordinatorlayout 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:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" > <android.support.design.widget.appbarlayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/appbar_padding_top" android:theme="@style/apptheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/apptheme.popupoverlay"> </android.support.v7.widget.toolbar> </android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
my fragment_generaldiscription.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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.user.myapp.generaldiscription$placeholderfragment"> <textview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:text="fffff" />
edit generaldiscription.java
import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class generaldiscription extends appcompatactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link fragmentpageradapter} derivative, keep every * loaded fragment in memory. if becomes memory intensive, * may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ private sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ private viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_generaldiscription); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_generaldiscription, 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); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; public placeholderfragment() { } /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_generaldiscription, container, false); textview textview = (textview) rootview.findviewbyid(r.id.section_label); textview.settext(getstring(r.string.section_format, getarguments().getint(arg_section_number))); return rootview; } } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class below). return placeholderfragment.newinstance(position + 1); } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "section 1"; case 1: return "section 2"; case 2: return "section 3"; } return null; } }
}
start fragment way:
fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.main_content , new generaldiscription()) .commit();
edit
in case should start generaldiscription this:
intent myintent = new intent(mainactivity.this, generaldiscription.class); mainactivity.this.startactivity(myintent);
what more, generaldiscription should import android.support.v4.app.fragment; instead of android.app.fragment;
Comments
Post a Comment