如何从activity2的片段调用activity1的片段

时间:2015-09-04 02:26:44

标签: android android-fragments

我有2个活动,在activity1上我添加2个片段和activity2 so。如何从activity2的片段调用activity1的片段。 我使用fragmentTransaction.commit()来调用但是错误

java.lang.ClassCastException: MainActivity cannot be cast to   SignInWithEmailFragment$OnSignInWithEmailFragmentListener

enter image description here

2 个答案:

答案 0 :(得分:0)

package life.youlook.youlooklife.ui.more;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

import life.youlook.youlooklife.R;
import life.youlook.youlooklife.model.AllThing;
import life.youlook.youlooklife.model.More;
import life.youlook.youlooklife.services.GetYourInterestedAsyncTask;
import life.youlook.youlooklife.ui.BaseContainerFragment;
import life.youlook.youlooklife.ui.BaseFragment;
import life.youlook.youlooklife.ui.mainscreen.MainActivity;
import life.youlook.youlooklife.ui.mainscreen.OnChangeActionbarListener;
import life.youlook.youlooklife.ui.startscreen.SignInWithEmailFragment;

/**
 * Created by Welcome on 9/1/2015.
 */
public class MoreFragment extends BaseFragment implements GetYourInterestedAsyncTask.OnGetYourInterestedListener
        , MainActivity.OnChangeActionbarFromActivity{
    ArrayList<More> arrayMore;
    MoreAdapter moreAdapter;
    ListView lvMore;
    Button btnCreateTopic;
    private OnChangeActionbarListener onChangeActionbarListener;



    @Override
    public void init() {
        onChangeActionbarListener = (OnChangeActionbarListener) getActivity();
        lvMore = (ListView) getView().findViewById(R.id.fragment_more_lvMore);
        btnCreateTopic =(Button) getView().findViewById(R.id.fragment_more_btnCreateTopic);
        arrayMore = new ArrayList<>();
        moreAdapter = new MoreAdapter(getActivity(),R.layout.row_more, arrayMore,this,"More");
        lvMore.setAdapter(moreAdapter);
    }

    @Override
    public void setValue() {
        arrayMore.add(new More(R.drawable.more_update, getResources().getString(R.string.more_update)));
        arrayMore.add(new More(R.drawable.more_notifications, getResources().getString(R.string.more_notification)));
        arrayMore.add(new More("SUPPORT"));
        arrayMore.add(new More(R.drawable.more_report, getResources().getString(R.string.more_report)));
        arrayMore.add(new More(R.drawable.more_term, getResources().getString(R.string.more_term)));
        arrayMore.add(new More(R.drawable.more_privace, getResources().getString(R.string.more_privace)));
        arrayMore.add(new More(R.drawable.more_language, getResources().getString(R.string.more_language)));
        arrayMore.add(new More(R.drawable.more_about, getResources().getString(R.string.more_about)));
        arrayMore.add(new More(R.drawable.more_logout, getResources().getString(R.string.more_logout)));

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_more;
    }

    @Override
    public void setEvent() {
        onChangeActionbarListener.changeTitle(getTitle(), this);
        btnCreateTopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lvMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(arrayMore.get(position).getmText().equals("Log Out")){
                    SignInWithEmailFragment email = new SignInWithEmailFragment();
                            ((BaseContainerFragment) MoreFragment.this.getParentFragment()).replaceFragment(email, false);

                }
            }
        });
    }

    @Override
    public String getTitle() {
        return "Collection";
    }

    @Override
    public void OnGetYourInterestedFinish(ArrayList<AllThing> arrayAll) {

    }
}

答案 1 :(得分:0)

ProfileFragment mProfileFragment = new ProfileFragment(); 
FragmentManager fragmentManager = getFragmentManager();  
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();  
fragmentTransaction.replace(R.id.content_frame,  
mProfileFragment);
fragmentTransaction.addToBackStack(MyProfileFragment.class
.getName());
fragmentTransaction.commit();