gpt4 book ai didi

android - 从内部重新加载 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:03 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何从 Fragment 中定义的菜单按钮刷新我的 Fragment。

我看过this question here但我希望有一个更详细的例子来说明我将如何实现这一目标。

任何帮助或指导将不胜感激,谢谢。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.clear_cache:
mImageFetcher.clearCache();
Toast.makeText(getActivity(), R.string.clear_cache_complete_toast,
Toast.LENGTH_SHORT).show();
break;

case R.id.random:

//Reload the fragment from here

getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.gridView, new Fragment3())
.commit();


return true;
}
return super.onOptionsItemSelected(item);

XML:

        <activity
android:name="com.stackoverflow.question.ImageGridActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >
</activity>

崩溃:

12-04 09:48:27.636: E/AndroidRuntime(20461): FATAL EXCEPTION: main
12-04 09:48:27.636: E/AndroidRuntime(20461): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.widget.AdapterView.addView(AdapterView.java:451)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.os.Handler.handleCallback(Handler.java:615)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.os.Looper.loop(Looper.java:213)
12-04 09:48:27.636: E/AndroidRuntime(20461): at android.app.ActivityThread.main(ActivityThread.java:4787)
12-04 09:48:27.636: E/AndroidRuntime(20461): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 09:48:27.636: E/AndroidRuntime(20461): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 09:48:27.636: E/AndroidRuntime(20461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-04 09:48:27.636: E/AndroidRuntime(20461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-04 09:48:27.636: E/AndroidRuntime(20461): at dalvik.system.NativeStart.main(Native Method)

当使用@Matthew Mcveigh 回答时:

12-04 10:05:09.315: E/AndroidRuntime(21072): FATAL EXCEPTION: main
12-04 10:05:09.315: E/AndroidRuntime(21072): java.lang.NullPointerException
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:666)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.os.Handler.handleCallback(Handler.java:615)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.os.Looper.loop(Looper.java:213)
12-04 10:05:09.315: E/AndroidRuntime(21072): at android.app.ActivityThread.main(ActivityThread.java:4787)
12-04 10:05:09.315: E/AndroidRuntime(21072): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 10:05:09.315: E/AndroidRuntime(21072): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 10:05:09.315: E/AndroidRuntime(21072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-04 10:05:09.315: E/AndroidRuntime(21072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-04 10:05:09.315: E/AndroidRuntime(21072): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在 Fragment 里面放这些:

private Callbacks mCallbacks;


public interface Callbacks {
//Callback for when button clicked.
public void onButtonClicked();
}


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks
mCallbacks = (Callbacks) activity;

}

在 onOptionsItemSelected 中更改为

case R.id.random:

mCallbacks.onButtonClicked();
break;

让你的 FragmentActivity 实现 YourFragment.Callbacks :

public class YourActivityName extends FragmentActivity implements YourFragment.Callbacks

在包含 Fragment 的 FragmentActivity 中添加:

    @Override
public void onButtonClicked() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.gridView, new Fragment3())
.commit();

}

关于android - 从内部重新加载 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357557/

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