如何从片段中调用MainActivity中的视图?

时间:2017-11-28 10:00:28

标签: java android android-fragments

我有一个活动说MyActivity,其中ViewPager设置了FragmentStatePagerAdapter,可以调整给定数量的碎片。另外我想在所有片段上添加CirclePageIndicator(Jake wharton's)。

我的问题是:我的ViewPager位于MyActivity.xml文件中,但我的CirclePageIndicator视图位于fragment.xml文件中。 为了触发CirclePageIndicator,我需要使用setViewPager方法,该方法需要ViewPager对象作为参数,这意味着我需要触发CirclePageIndicator MyActivity 1}}我初始化ViewPager obj。

的类

尽管CirclePageIndicator文件位于MyActivity,我如何安全地拨打fragment.xml pageFragment = (PageFragment) getSupportFragmentManager().findFragmentById(R.id.fragId); pageFragment.circleIndicator.setViewPager(viewPager);

重要的是要注意,我发现了一个类似的问题,他们建议像这样访问片段:

Caused by: java.lang.NullPointerException: Attempt to read from field 'com.viewpagerindicator.CirclePageIndicator com.example.ibm.shoppingequalizer.appintrolib.PageFragment.circleIndicator' on a null object reference
                                                                                   at com.example.ibm.shoppingequalizer.appintrolib.MyActivity.onCreate(MyActivity.java:32)

但是在尝试访问circleIndicator时我仍然收到了nullPointerException !!如下:

public class AppIntro extends FragmentActivity {

ViewPager viewPager;
PageFragment pageFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_intro2);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    PageFragment.SwipeAdapter swipeAdapter = new PageFragment.SwipeAdapter(getSupportFragmentManager());
    viewPager.setAdapter(swipeAdapter);


    //In order to use CirclePageIndicator I need 
    //something like this ->  circleIndicator.setViewPager(viewPager);

 }
}  

这是我的代码:

MyActivity.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ibm.shoppingequalizer.appintrolib.MyActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

my_activity.xml

public class PageFragment extends Fragment {

public static PageFragment newInstance(String title, String msg, int thumb) {
    PageFragment fragment = new PageFragment();

    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("msg", msg);
    args.putInt("thumb", thumb);
    fragment.setArguments(args);

    return fragment;
}

TextView message;
TextView title;
ImageView imageView;
CirclePageIndicator circleIndicator;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_page, container, false);

    String title1 = getArguments().getString("title");
    String msg = getArguments().getString("msg");


    message = (TextView) view.findViewById(R.id.msg);
    title = (TextView) view.findViewById(R.id.title);
    imageView = (ImageView) view.findViewById(R.id.imageView);
    circleIndicator = (CirclePageIndicator) view.findViewById(R.id.circleIndicator);

    setTextFonts();

    title.setText(title1);
    message.setText(msg);



    return view;
   }
}

MyFragment.java

static class SwipeAdapter extends FragmentStatePagerAdapter {

    List<IntroPageInfo> allInfo = getItem();

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

    public List<IntroPageInfo> getItem() {
        List<IntroPageInfo> allInfo = new ArrayList<>();

        allInfo.add(new IntroPageInfo("A",
                "blalalala",
                R.drawable.grocery_shopping_with_the_holy_spirit));
        allInfo.add(new IntroPageInfo("B",
                "didndiddidind",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("C",
                "ciccicicicici",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("D",
                "ding dang dong",
                R.drawable.category_intro));
        allInfo.add(new IntroPageInfo("E",
                "ehmmmmmmmmmmmmmmm",
                R.drawable.category_intro));

        return allInfo;
    }


    @Override
    public Fragment getItem(int i) {
        IntroPageInfo info = allInfo.get(i);
        PageFragment fragment = new PageFragment().newInstance(info.title, info.message, info.image);
        return fragment;
    }

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

SwipeAdapter.java

static class IntroPageInfo {
    String title;
    String message;
    int image;

    public IntroPageInfo(String title, String message, int image) {
        this.title = title;
        this.message = message;
        this.image = image;
    }
}

IntroPageInfo.java

  ReDim Preserve resultArray(1 to 20) As Double

2 个答案:

答案 0 :(得分:0)

如果您想要调用Fragment中的Activity视图,

尝试在activity中实现此功能,以提供通过接口

从此活动调用视图的方法
public class TestActivity implement GetTextCallback {
   public interface GetTextCallback {
       String getTextYouWant()
   }

   @Override
   public String getTextYouWant() {
       return editText.getText().toString();
   }
}

并在片段中,通过您创建的界面从您的活动中调用视图

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (getTextCallback == null) {
        getTextCallback = (GetTextCallback) activity;
    }
}

public void whenYouWantToGetText() {
    if (getTextCallback != null) {
        getTextCallback.getTextYouWant();
    } 
}

答案 1 :(得分:0)

您应该在活动中加入CirclePageIndicator

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_intro2);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    PageFragment.SwipeAdapter swipeAdapter = new PageFragment.SwipeAdapter(getSupportFragmentManager());
    viewPager.setAdapter(swipeAdapter);
    circleIndicator = (CirclePageIndicator) findViewById(R.id.circleIndicator);
    circleIndicator.setupWithViewPager(viewPager);


 }

也相应地修改 xml文件。 从<{1}}移动 circleIndicatorfragment xml文件。 activity骨架应该是这样的:

activity.xml