getActivity()在FragmentActivity:机器人 [英] getActivity() with in FragmentActivity: android

查看:120
本文介绍了getActivity()在FragmentActivity:机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的这个A级延伸另一个抽象类(而这个抽象类扩展FragmentActivity),并在一个我在一类我想getActivity()我目前的活动A.功能,但每当我用getActivity ,它给了我错误getActivity()方法没有定义类型,我的课。请帮我 !!我怎样才能做到这一点?

code我的A级

 大众A级扩展乙
 {
  保护无效的onCreate(包savedInstanceState)
   {
    super.onCreate(savedInstanceState);


    的setContentView(R.layout.voicing);

    //做一套的初始化和东西//

   }
  }
 

code为B类

 公共抽象类B扩展FragmentActivity
  {


   FragmentAdapter mAdapter;
   ViewPager mPager;
   PageIndicator mIndicator;


 }
 

解决方案

A FragmentActivity 活动的一个实​​例 (在Java中的风格: FragmentActivity延伸活动

所以没有点呼叫 getActivity ,因为只调用/使用将工作。也没有办法叫 getActivity FragmentActivity 类。换句话说,你的B级也延长了活动实例。

所以,在你的A(或B)类,你可以使用这个code段得到活动实例:

 活动试验=(活动)这一点;
 

但如果你的B级正把片段 getActivity 调用会起作用。

I am using this class A which extends another abstract class (and this abstract class extends FragmentActivity) and in one of my function with in A class I want to get getActivity() for my current activity A. But whenever I use getActivity , It gives me error that getActivity() method is not defined type for my class. Please help me !! How can I achieve this??

Code for my class A

 public class A extends B
 {
  protected void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.voicing);

    //doing another initializations and stuff//

   }
  }

code for class B

   public abstract class B extends FragmentActivity 
  {


   FragmentAdapter mAdapter;
   ViewPager mPager;
   PageIndicator mIndicator;


 }

解决方案

A FragmentActivity is an instance of Activity (in Java style: FragmentActivity extends Activity).

So there is no point calling getActivity because just calling/using this will work. Also there is no method called getActivity in the FragmentActivity class. In other words your B class is also extending an Activity instance.

So inside your A (or B) class you could use this code snippet to get the activity instance:

Activity test = (Activity) this;

But if your B class is extending a Fragment then the getActivity call would have worked.

这篇关于getActivity()在FragmentActivity:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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