android - Start an activity from a fragment -


i have 2 fragments on both fragments button. when press button i'd start new activity. can't work.

the error i'm getting: error here: type mismatch: cannot convert mfragmentfavorite fragment

what doing wrong?

myfragmentpageradapter

import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter;  public class myfragmentpageradapter extends fragmentpageradapter{      final int page_count = 3;      /** constructor of class */     public myfragmentpageradapter(fragmentmanager fm) {         super(fm);     }      /** method invoked when page requested create */     @override     public fragment getitem(int arg0) {          switch(arg0){          case 0:             return new favoriteactivity();                     //error: type mismatch: cannot convert favoriteactivity fragment           case 1:             return new settingsactivity();           default:             return null;          }            }      /** returns number of pages */     @override     public int getcount() {         return page_count;     } } 

favoriteactivity

import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button;  public class favoriteactivity extends activity{      public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         view v = inflater.inflate(r.layout.main_favorite, container, false);           onclicklistener listnr=new onclicklistener() {             @override             public void onclick(view v) {                   intent i= new intent("afavorite");                   startactivity(i);             }         };            button btn =(button) v.findviewbyid(r.id.mainfavorite);           btn.setonclicklistener(listnr);            return v;     } } 

if favoriteactivity extends fragments, error gone error @ findviewbyid(r.id.mainfavorite); , error is

the method findviewbyid(int) undefined type favoriteactivity

mfragmentfavorite in code fragmentactivity not same thing fragment. that's why you're getting type mismatch. also, should never call new on activity not proper way start one.

if want start new instance of mfragmentfavorite, can via intent.

from fragment:

intent intent = new intent(getactivity(), mfragmentfavorite.class); startactivity(intent); 

from activity

intent intent = new intent(this, mfragmentfavorite.class); startactivity(intent); 

if want start afavorite instead of mfragmentfavorite need change out names in created intent.

also, recommend changing class names more accurate. calling mfragmentfavorite improper in it's not fragment @ all. also, class declarations in java typically start capital letter. you'd name class favoriteactivity more accurate , conform language conventions. need rename file favoriteactivity.java if choose since java requires class names match file name.

update

also, looks meant formfragmentfavorite fragment instead of fragmentactivity based on use of oncreateview. if want mfragmentfavorite fragment change following line of code:

public class mfragmentfavorite extends fragmentactivity{ 

make instead read:

public class mfragmentfavorite extends fragment { 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -