java - Unable to Start an activity when click from fragment performed -
all want on click image practice fragment open activity_lego. tried know, tried google's advice. nothing works fine.
main_activity.java
package com.example.wolfknight.tabbed; import android.support.design.widget.tablayout; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.support.v4.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; public class mainactivity extends appcompatactivity { private sectionspageradapter msectionspageradapter; private viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public static class placeholderfragment extends fragment { private static final string arg_section_number = "section_number"; public placeholderfragment() { } 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) { if (getarguments().getint(arg_section_number) == 1) { view rootview = inflater.inflate(r.layout.fragment_practice, container, false); return rootview; } else if (getarguments().getint(arg_section_number) == 2) { view rootview = inflater.inflate(r.layout.fragment_study, container, false); return rootview; } else { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return placeholderfragment.newinstance(position + 1); } @override public int getcount() { return 2; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "class"; case 1: return "practice"; } return null; } } }
and.... fragment>>practice.java
package layout; import android.content.context; import android.content.intent; 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; import android.widget.button; import android.widget.imageview; import com.example.wolfknight.tabbed.r; import com.example.wolfknight.tabbed.lego; public class practice extends fragment { private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; private string mparam1; private string mparam2; private onfragmentinteractionlistener mlistener; public practice() {} public static practice newinstance(string param1, string param2) { practice fragment = new practice(); 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) { view view = inflater.inflate(r.layout.fragment_practice, container, false); imageview mbutton = (imageview) view.findviewbyid(r.id.image); mbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = new intent(getactivity(), lego.class); startactivity(intent); } }); return view; } 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; } public interface onfragmentinteractionlistener { void onfragmentinteraction(uri uri); } }
xml part works fine.. app runs nothing respond on click..
try setting button's onclicklistener
in onviewcreated
instead.
Comments
Post a Comment