设置并调用Fragment Tag正确

时间:2017-07-25 09:38:40

标签: android android-fragments

无法摆脱这个问题。我看到了一些类似问题的其他问题,但没有修复它。

简短的故事:我用MainActivty中的片段替换了占位符,并希望从该新片段中调用公共方法。但是从他的Tag中调用片段会导致一个" Null对象引用"错误。 我尝试了多种方法来设置和调用标签,但没有成功

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/mainActivity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:menu="@menu/quick_settings"
tools:context="....">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

        <LinearLayout
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

        <LinearLayout
            android:id="@+id/fragment3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
        ...

Main.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_annual_service);
    createList();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void createList() {

    Resources r = getResources();
    String packageName = getPackageName();

    //Array with all Fragments
    fragmentList = new Fragment[]{
                new FragmentA,
                new FragmentB,
                new FragmentC,
               ...
        };
    numberOfFragments = fragmentList.length;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    for (int arrayIndex=0, fragmentNumber=1; fragmentNumber <= numberOfFragments; arrayIndex++, fragmentNumber++){

        fragmentTransaction.replace(r.getIdentifier("fragment" + fragmentNumber, "id", packageName), fragmentList[arrayIndex], "fragment0"+fragmentNumber); // also try to set the TagName

    }
    fragmentTransaction.commit();
    fragmentManager.executePendingTransactions();
    ((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold(); // Method is implemented by a interface

FragmentA.xml

<?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:id="@+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingEnd="20dp"
        android:paddingStart="45dp"
        android:visibility="gone">
  ...
</LinearLayout>

FragmentA.java

Button backBtn;
TextView header;
LinearLayout llayout;
Drawable defaulBG;
View view;

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

    return setListener(inflater, container);
}

private View setListener(LayoutInflater inflater, ViewGroup container){
    view = inflater.inflate(R.layout.fragment_general_information, container, false);
    header = (TextView) view.findViewById(R.id.generalInformationHeaderText);
    header.setOnClickListener(this);
    llayout = (LinearLayout) view.findViewById(R.id.content);
    defaulBG = inForkliftBtn.getBackground();
    backBtn = (Button) view.findViewById(R.id.backBtn);
    setRetainInstance(true);
    return view;
}

public void unfold(){
    if(llayout.getVisibility() == View.GONE){
        llayout.setVisibility(View.VISIBLE);
    }else{
        llayout.setVisibility(View.GONE);
    }
}

1 个答案:

答案 0 :(得分:1)

提交事务后在片段管理器上调用executePendingTransactions()

...
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold();

编辑1

您可以尝试其他选项:

  1. 添加executePendingTransactions()

  2. 在片段的onViewCreated中,如果getTag()匹配&#34; fragment01&#34;致电unfold方法