android - 非常基本的 actionbarsherlock 带有 fragment 的标签 : FragmentTransaction is null in the onTabSelected() method

标签 android tabs android-fragments actionbarsherlock

我正在实现示例中的一些代码 (http://arvid-g.de/12/android-4-actionbar-with-tabs-example) 并尝试将其转换为与 ActionBarSherlock 一起使用。

这里是 TabActivity.java:

package com.pnet;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.Toast;

public class TabActivity extends SherlockActivity {

    private static String TAG = "TabActivity";
    public static Context appContext;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_activity);

       //ActionBar gets initiated
        ActionBar actionbar = getSupportActionBar();
      //Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      //initiating both tabs and set text to it.
        ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
        ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

     //create the two fragments we want to use for display content
        Fragment PlayerFragment = new AFragment();
        Fragment StationsFragment = new BFragment();

    //set the Tab listener. Now we can listen for clicks.
        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
        StationsTab.setTabListener(new MyTabsListener(StationsFragment));

   //add the two tabs to the actionbar
        actionbar.addTab(PlayerTab);
        actionbar.addTab(StationsTab);
    }

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            if (fragment == null) {
                Log.v(TAG, "fragment is null");
            }

            if (ft == null) {
                Log.v(TAG, "fragment TRANSACTION is null");
            }

            ft.replace(R.id.fragment_container, fragment);          
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            Toast.makeText(TabActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();

        }

    }
}

这里是AFragment.java

package com.pnet;

import com.actionbarsherlock.app.SherlockFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AFragment extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.afragment, container, false);
    }

}

这里是BFragment.java

package com.pnet;

import com.actionbarsherlock.app.SherlockFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BFragment extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.bfragment, container, false);
    }

}

这是 tab_activity.xml:

<LinearLayout android:layout_gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/fragment_container" android:layout_height="match_parent" android:layout_width="match_parent">

 </LinearLayout>
</LinearLayout>

这是 afragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/hello_world"/>


</LinearLayout>

这里是bfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/tab_navigation_content"/>


</LinearLayout>

当 Activity 加载时,它会触发 onTabSelected() 方法。我放置了几个 if 语句检查写入日志的对象是否为 null,它表明 FragmentTransaction 对象为 null。有人看到我哪里出错了吗?谢谢。

最佳答案

您应该使您的 Activity 从 SherlockFragmentActivity 而不是 SherlockActivity 扩展,就像您使用 FragmentActivity 而不是带有 support-v4 包的 Activity 一样

请在此处查看 fragment 示例:https://github.com/JakeWharton/ActionBarSherlock/tree/master/actionbarsherlock-samples/fragments

关于android - 非常基本的 actionbarsherlock 带有 fragment 的标签 : FragmentTransaction is null in the onTabSelected() method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073850/

相关文章:

android - 如何将第二个启动器图标添加到以前在市场上发布的只有一个启动器图标的应用程序?

java - 膨胀 TableLayout 时出错

Android:用另一个 View 替换一个 View

android - 并行运行两个 AsyncTaskLoader

css - Bootstrap : prevent "nav-tabs nav-justified" from stacking on small screen

python - Tkinter 中的笔记本小部件

android - 我们如何在从 android spinner 选择的项目上传递与我们在 spinner 中选择的文本不同的值?

android - 如何在应用程序重启之间维护 parse.com 自动用户?

android - 将 ParseObject 中的特定值用于新查询

Android:使用选项卡在不同 fragment 之间切换