如何从Android片段更新XML TextView?

时间:2013-01-06 07:36:22

标签: android android-fragments textview

我正在尝试从TextView更新片段FragmentActivity的文本。在else语句中,我首先创建Fragment,然后使用属于该片段的TextView方法更新updateItemView(),但所有我得到的是一个 空指针异常。

我可以从TextView语句更新if,但为什么我无法更新TextView 来自else声明?

这是FragmentActivity

的代码
public void toDeatailsBtn(View view){

    ItemFragment listFragment = (ItemFragment) getSupportFragmentManager()
    .findFragmentById(R.id.large_layout_list_item_fragment);

    if(listFragment != null){
        //The "if" code is working.
        listFragment.updateItemView();
    }

    // I'm swaping fragments here. I already added firstFragment to the
    //fragment_container FrameLayout in other code.
    else{
        ItemFragment secondFragment = new ItemFragment();
        FragmentTransaction transaction =
        getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, secondFragment);

        transaction.addToBackStack(null);
        transaction.commit();

        //This is giving me the null pointer exception, even though
        //secondFragment has been created in theory.
        secondFragment.updateItemView();
    }
}

包含Fragment updateItemView()的{​​{1}}:

Method

public class ItemFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.item_fragment, container, false); } public void updateItemView(){ TextView name = (TextView) getActivity() .findViewById(R.id.list_item_fragment); name.setText("TEST"); } }

TextView

最后我用<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/list_item_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:freezesText="true" /> </LinearLayout> </ScrollView> 代替片段:

FrameLayout

错误日志:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainFragmentActivity" />

2 个答案:

答案 0 :(得分:0)

当您调用commit时,不会立即执行Fragment事务,因此当您调用TextView时,无法立即找到updateItemView()。尝试使用FragmentManager.executePendingTransaction()强制进行交易:

//...
transaction.addToBackStack(null);
transaction.commit();
getSupportFragmentManager().executePendingTransaction();
secondFragment.updateItemView();

答案 1 :(得分:0)

myFragment类中编写一个静态方法:

public static myFragment newInstance(String txt) {
        myFragment myfragment = new myFragment();

        Bundle args = new Bundle();
        args.putString("TEXT", txt);
        myfragment.setArguments(args);

        return myfragment;
    }
之后OnCreate myFragment inflate textView tv tv.setText(getArguments().getString("TEXT"));

FragmentActivity方法
myFragment fragment = myFragment.newInstance(textToBeSent);

在主replace课程中执行:

fragmentTransaction.replace(R.id.fr_dynamic, fragment);
fragmentTransaction.commit();

之前的{{1}},

{{1}}