ITKeyword,专注技术干货聚合推荐

注册 | 登录

Android Activity with Fragments, findFragment always returns null

分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android Fragments详解五:与activity通讯

与activity通讯   尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。   Fragment可

I'm dealing with and Android app using an activity and 3 fragments with a ViewPager and a FragmentPagerAdapter. The app is working correctly but I still cannot retrieve the reference of a Fragment within the Activity. The methods findFragmentById and findFragmentbyTag always return null. Here's my code: FragmentPagerAdapter:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {

            // Open FragmentTab1.java
            case 0:
                FragmentTab1 fragmenttab1 = new FragmentTab1();
                return fragmenttab1;

            // Open FragmentTab2.java
            case 1:
                FragmentTab2 fragmenttab2 = new FragmentTab2();
                return fragmenttab2;

            // Open FragmentTab3.java
            case 2:
                FragmentTab3 fragmenttab3 = new FragmentTab3();
                return fragmenttab3;
        }
        return null;
    }
}

Fragment implementation:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;

public class FragmentTab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragmenttab1, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }
}

And finally my activity:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        // Locate the viewpager in activity_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        // Set the ViewPagerAdapter into ViewPager
        viewPager.setAdapter(viewPagerAdapter);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        InitializeVariables();
        viewPager.setCurrentItem(1);
        Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentByTag(makeFragmentName(viewPager.getId(), 0));  
    }
android android-fragments fragmentpageradapter
|
  this question
asked Feb 4 '16 at 9:38 Francesco 555 1 11 36      viewPager.getItem(1), try with that –  Raphael Teyssandier Feb 4 '16 at 9:39      use findFragmentByTag and make sure all your fragments are tagged. –  Shark Feb 4 '16 at 9:44      For findFragmentbyTag it's normal that you get null because you haven't set the tags for your fragments (or at least we don't know since you didn't post the xml layouts). And i think this also applies on findFragmentById ... –  Kalem Feb 4 '16 at 9:47

 | 

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。