简体   繁体   English

带有意图的片段中的 onclicklistener

[英]onclicklistener in fragment with intent

Good morning together,一起早安,

i have an an android app with an Intro class.我有一个带有 Intro 类的 android 应用程序。 This Intro class has got three fragments.这个介绍类有三个片段。

in fragment 3 (IntroPage3) i would like to set an onclicklistener with an intent form the IntroPage3 to Overview.class like this:在片段 3 (IntroPage3) 中,我想设置一个 onclicklistener,其意图是从 IntroPage3 到 Overview.class,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FragementView = inflater.inflate(R.layout.intro_page1, container, false);

    Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
    FinishIntroButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            intent = new Intent(IntroPage3.this, Overview.class);
            startActivityForResult(intent, 0);
        }
    });


    return FragementView;
}

Problem: is this line:问题:这一行是:

intent = new Intent(IntroPage3.this, Intro.class);意图 = 新意图(IntroPage3.this, Intro.class);

Error message:错误信息: 在此处输入图片说明

Any ideas?有任何想法吗? :) :)

使用intent = new Intent(getActivity(), Overview.class);

You must use getActivity() instead of IntroPage3.this您必须使用getActivity()而不是IntroPage3.this

WHY为什么

getActivity() in a Fragment returns the Activity the Fragment is currently associated with. Fragment 中的getActivity()返回 Fragment 当前关联的 Activity。

Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
FinishIntroButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        intent = new Intent(getActivity(), Overview.class);
        startActivityForResult(intent, 0);
    }
});

public class Fragment3 extends Fragment{公共类 Fragment3 扩展 Fragment{

@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view;
    view = inflater.inflate(R.layout.page_4, container, false);
    Button button=view.findViewById(R.id.exit);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent intent;
          intent=new Intent(getActivity(),FragmentLogin.class);
          startActivity(intent);

        }
    });




    return view;


}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM