使用选项卡滑动视图:Activity 或片段?

Swipe Views with Tabs : Activity or Fragment?

我正在尝试使用选项卡(3 个选项卡)对滑动视图进行编码。我希望每个 'page' 都是一个 Activity。 我正在使用这个 tutorial 中的代码,但不幸的是每个 'page' 只能有一个片段。

我尝试做的是将我的 Activity 转换为片段(但我仍然遇到一些 NullPointerException)。

使用 Fragment 代替 Activity 是否安全(我在其中使用:AlarmManager、OnCheckedChangeListener、PackageManager、AlertDialog...)? 有没有更好的方法来解决我的问题?

谢谢。

============================================= =========================

(更新 1)

下面是转换后的 Fragment 中的一些代码:

public class TabFragment extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

private HashMap<String, String> myMap;
static private TextView usernameView;
private String mUsername;
private String permPassword;
private SharedPreferences pref;
private SharedPreferences prefCheckBox;
private TextView trackTextView;
private TextView logoutTextView;
private TextView settingsTextView;
private CheckBox trackCheckBox;
boolean isTrackingEnabled;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =  inflater.inflate(R.layout.activity_home_profile, container, false);

    final SessionManager session = new SessionManager(getActivity().getApplicationContext());
    myMap = session.getUserDetails();
    usernameView = (TextView) getActivity().findViewById(R.id.usernameText);
    mUsername = myMap.get("username");
    usernameView.setText("");
    usernameView.setText(mUsername);

    trackTextView = (TextView) view.findViewById(R.id.tracking_textView);
    trackCheckBox = (CheckBox) view.findViewById(R.id.track_checkBox);
    logoutTextView = (TextView) view.findViewById(R.id.log_out);
    settingsTextView = (TextView) view.findViewById(R.id.settings);

    prefCheckBox = getActivity().getApplicationContext().getSharedPreferences("Tracking Checkbox", 0); // 0 - for private mode
    isTrackingEnabled = prefCheckBox.getBoolean("CheckBoxState", false);
    // display checkbox previous state
    trackCheckBox.setChecked(isTrackingEnabled);

    logoutTextView.setOnClickListener(this);
    trackTextView.setOnClickListener(this);
    settingsTextView.setOnClickListener(this);

    trackCheckBox.setOnCheckedChangeListener(this);

    return view;
}

我收到 usernameView.setText("")

的 NullPointerException
02-10 15:03:35.203  27866-27866/com.projectname E/AndroidRuntime﹕ FATAL EXCEPTION: main

java.lang.NullPointerException

您不能在单个页面中使用多个 activity。

唯一的方法是片段,构建片段以支持此功能。

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

将Activity转换成片段非常简单

  1. 如果 activity 您正在扩展 Activity 将其更改为 Fragment
  2. 将您的 onCreate 函数更改为 onCreateView

例如:

Activity-

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }   
    }

转换片段-

public class MainActivity extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View  view=inflater.inflate(R.layout.activity_main,null);

        return view;
    }
}

最重要

在activity里面我们通常习惯用这种方法从xml找view

TextView textView= (TextView) findViewById(R.id.helletxtview);

在片段内部这将不起作用,因为 findViewById 在片段中不可用 class,但是可以像这样在根视图的帮助下使用

View view=inflater.inflate(R.layout.activity_main,null);
TextView textView= (TextView) view.findViewById(R.id.helletxtview);