gpt4 book ai didi

安卓删除 fragment

转载 作者:行者123 更新时间:2023-11-29 00:35:47 28 4
gpt4 key购买 nike

如何分离或删除 FragmentTransaction?谁能告诉我如何分离它的例子。我试过分离和删除。它仍然无法正常工作。请帮我。请注意,我在我的代码中使用了 asynctask。

    @Override
public void onActivityCreated(Bundle savedInstanceState) {

new loadListView().execute();

super.onActivityCreated(savedInstanceState);
}

public class loadListView extends AsyncTask<Integer, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(Integer... args) {
............
}


@Override
protected void onPostExecute(String args) {

if (getActivity() != null) {
adapter=new MainPageLazyAdapter(getActivity(), menuItems);
setListAdapter(adapter);



}
}


@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {



FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, this);
fragmentTransaction.attach(this);
fragmentTransaction.commit();
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.detach(this);
fragmentTransaction.commit();



}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}


}

洛卡特:

10-09 14:49:42.305: E/AndroidRuntime(5731): FATAL EXCEPTION: main
10-09 14:49:42.305: E/AndroidRuntime(5731): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.NullPointerException
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.os.Looper.loop(Looper.java:137)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread.main(ActivityThread.java:4512)
10-09 14:49:42.305: E/AndroidRuntime(5731): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 14:49:42.305: E/AndroidRuntime(5731): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-09 14:49:42.305: E/AndroidRuntime(5731): at dalvik.system.NativeStart.main(Native Method)
10-09 14:49:42.305: E/AndroidRuntime(5731): Caused by: java.lang.NullPointerException
10-09 14:49:42.305: E/AndroidRuntime(5731): at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment.onTabSelected(AndroidFragment.java:170)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:344)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:512)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:441)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:427)
10-09 14:49:42.305: E/AndroidRuntime(5731): at com.actionbarsherlock.internal.app.ActionBarWrapper.addTab(ActionBarWrapper.java:377)
10-09 14:49:42.305: E/AndroidRuntime(5731): at in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity.onCreate(MainActivity.java:24)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.Activity.performCreate(Activity.java:4465)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-09 14:49:42.305: E/AndroidRuntime(5731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-09 14:49:42.305: E/AndroidRuntime(5731): ... 11 more

最佳答案

这是错误的,请参阅我的更新。

您需要调用commit()添加或删除 fragment 后在 FragmentTransaction 上。

例子:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, yourFragment);
fragmentTransaction.commit();

更新

根据文档,您不必提交事务,系统会为您完成:http://developer.android.com/guide/topics/ui/actionbar.html

这是一个运行良好的极简示例。您的 Activity 必须扩展 SherlockFragmentActivity(或 FragmentActivity)。该 Activity 实现了 TabListener

主要 Activity :

public class MainActivity extends SherlockFragmentActivity implements TabListener
{
private ActionBar.Tab homeTab;
private ActionBar.Tab tagsTab;
private FragOne fragOne;
private FragTwo fragTwo;


@Override
public void onCreate(Bundle b)
{
super.onCreate(b);

ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

homeTab = bar.newTab().setText("Home");
tagsTab = bar.newTab().setText("Tags");

homeTab.setTabListener(this);
tagsTab.setTabListener(this);

fragOne = FragOne.getInstance();
fragTwo = FragTwo.getInstance();

bar.addTab(homeTab);
bar.addTab(tagsTab);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (tab == homeTab)
ft.replace(android.R.id.content, fragOne);
else
ft.replace(android.R.id.content, fragTwo);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}

FragOne(另一个 fragment FragTwo 只是布局不同):

public class FragOne extends SherlockFragment
{
private static FragOne instance;


public static FragOne getInstance()
{
if (instance == null)
instance = new FragOne();
return instance;
}

private FragOne() {}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.frag_one_layout, container, false);
}
}

关于安卓删除 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12785188/

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