在Android编程中无法从Fragment切换到Activity / Activity到Fragment [英] Unable to switch from Fragment to Activity / Activity to Fragment in Android Programming

查看:231
本文介绍了在Android编程中无法从Fragment切换到Activity / Activity到Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我主要使用Fragments连接到Facebook。

Currently, I am mainly using Fragments to connect to Facebook.

然而,对于其他代码,我使用的是普通活动(没有碎片)。

However, for the other codes, I am using normal activites (no Fragments).

我现在的问题是,我希望有一个按钮可以从我的主页链接到片段,从片段链接到我的主页

My issue now is that I wish to have a button to link from my "Home Page" to the Fragment, and from the Fragment back to my "Home Page"

我无法这样做。

我尝试使用相同的代码在此活动之间切换,但它不起作用。

I tried to use the same code to switch between activities for this but it does not work.

有没有办法将正常活动链接到碎片和反之亦然?或者他们只能相互联系?

Is there a way to Link Normal Activities to Fragments and Vice Versa ? Or can they only link to each other ?

这是我的代码:

    public class SplashFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) {
    View view = inflater.inflate(R.layout.splash, container, false);
//   return view;

    Button btnNextScreen = (Button) view.findViewById(R.id.btnNextScreen);      
//    Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);

    //Listening to button event
    btnNextScreen.setOnClickListener(new View.OnClickListener() {

      public void onClick(View arg0) {
          //Starting a new Intent
          Intent nextScreen = new Intent(**getApplicationContext()**, SecondScreenActivity.class);
            startActivity(nextScreen);

}
});

 return view;   
}
}

我在getApplicationContext()时遇到错误。

I am getting an error at getApplicationContext().

如果我将其更改为getActivity(),他们将提示他们期望切换到片段而不是活动的另一个错误。

If I change it to getActivity(), they will prompt with another error that they are expecting to switch to a Fragment, not an activity.

感谢您的帮助!

问候,
AndroidStudent

Regards, AndroidStudent

推荐答案

如果你想从片段导航到活动,请使用此getActivity()。

use this getActivity() if u want to navigate from fragment to activity.

Intent nextScreen = new Intent(getActivity(), SecondScreenActivity.class);
startActivity(nextScreen);

这篇关于在Android编程中无法从Fragment切换到Activity / Activity到Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆