i tring implement tabs fragment , navigation view attacted while running project doesn't shows title tabs , getting error cannot resolve method getstring(int)
exploreactivity.java
package com.demo.www.demo; import android.app.searchmanager; import android.content.context; import android.net.uri; import android.support.design.widget.navigationview; import android.support.design.widget.tablayout; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v4.app.fragment; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.searchview; import android.support.v7.widget.toolbar; 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.menu; import android.view.menuitem; import android.view.view; public class exploreactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener, homefragment.onfragmentinteractionlistener, categoryfragment.onfragmentinteractionlistener, trendingfragment.onfragmentinteractionlistener { /** * {@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 fixedtabspageradapter mfixedtabspageradapter; /** * {@link viewpager} host section contents. */ private viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_explore); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); // create adapter return fragment each of 3 // primary sections of activity. mfixedtabspageradapter = new fixedtabspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(mfixedtabspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); 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(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_home, menu); // associate searchable configuration searchview searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service); searchview searchview = (searchview) menu.finditem(r.id.action_search).getactionview(); searchview.setsearchableinfo( searchmanager.getsearchableinfo(getcomponentname())); return true; } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @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_notification) { return true; } return super.onoptionsitemselected(item); } @override public void onfragmentinteraction(uri uri) { } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { // handle navigation view item clicks here. int id = item.getitemid(); if (id == r.id.nav_camera) { // handle camera action } else if (id == r.id.nav_gallery) { } else if (id == r.id.nav_slideshow) { } else if (id == r.id.nav_manage) { } else if (id == r.id.nav_share) { } else if (id == r.id.nav_send) { } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; } } class fixedtabspageradapter extends fragmentpageradapter { public fixedtabspageradapter(fragmentmanager fm) { super(fm); } @override public int getcount() { return 3; } @override public fragment getitem(int position) { switch (position) { case 0: return new homefragment(); case 1: return new categoryfragment(); case 2: return new trendingfragment(); default: return null; } } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return getstring(r.string.home_page_title); case 1: return getstring(r.string.category_page_title); case 2: return getstring(r.string.trending_page_title); default: return null; } } } homefragment.java package com.demo.www.demo; import android.content.context; import android.net.uri; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; /** * simple {@link fragment} subclass. * activities contain fragment must implement * {@link homefragment.onfragmentinteractionlistener} interface * handle interaction events. * use {@link homefragment#newinstance} factory method * create instance of fragment. */ public class homefragment extends 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; public homefragment() { // required empty public constructor } /** * use factory method create new instance of * fragment using provided parameters. * * @param param1 parameter 1. * @param param2 parameter 2. * @return new instance of fragment homefragment. */ // todo: rename , change types , number of parameters public static homefragment newinstance(string param1, string param2) { homefragment fragment = new homefragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } @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_home, 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(context context) { super.onattach(context); if (context instanceof onfragmentinteractionlistener) { mlistener = (onfragmentinteractionlistener) context; } else { throw new runtimeexception(context.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 void onfragmentinteraction(uri uri); } }
change
getstring(r.string.category_page_title);
to
getresources().getstring(r.string.home_page_title);
Comments
Post a Comment