gpt4 book ai didi

android - 如何从 Android fragment 更新 XML TextView?

转载 作者:太空狗 更新时间:2023-10-29 15:14:02 25 4
gpt4 key购买 nike

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

我可以从 if 语句更新 TextView 但为什么我不能更新 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();
}
}

包含updateItemView() 方法 fragment :

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>

最后是我用来替换 fragment 的 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" />

错误日志:

01-06 00:59:08.425: E/AndroidRuntime(1610): java.lang.IllegalStateException: Could not execute method of the activity
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.view.View$1.onClick(View.java:3591)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.view.View.performClick(View.java:4084)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.view.View$PerformClick.run(View.java:16966)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.os.Handler.handleCallback(Handler.java:615)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:137)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-06 00:59:08.425: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 00:59:08.425: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 00:59:08.425: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-06 00:59:08.425: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-06 00:59:08.425: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method)
01-06 00:59:08.425: E/AndroidRuntime(1610): Caused by: java.lang.reflect.InvocationTargetException
01-06 00:59:08.425: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 00:59:08.425: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 00:59:08.425: E/AndroidRuntime(1610): at android.view.View$1.onClick(View.java:3586)
01-06 00:59:08.425: E/AndroidRuntime(1610): ... 11 more
01-06 00:59:08.425: E/AndroidRuntime(1610): Caused by: java.lang.NullPointerException
01-06 00:59:08.425: E/AndroidRuntime(1610): at com.site.myapp.ItemFragment.updateItemView(ItemFragment.java:27)
01-06 00:59:08.425: E/AndroidRuntime(1610): at com.site.myapp.MainFragmentActivity.toDetailsBtn(MainFragmentActivity.java:90)
01-06 00:59:08.425: E/AndroidRuntime(1610): ... 14 more

最佳答案

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

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

关于android - 如何从 Android fragment 更新 XML TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com