[英]Swipe Views with Tabs : Activity or Fragment?
I'm trying to code a swipe views with tabs (3 tabs). 我正在尝试使用选项卡(3个选项卡)编写滑动视图的代码。 I want each 'page' to be an Activity.
我希望每个“页面”都是一个活动。 I'm using the code in this tutorial , but unfortunately each 'page' there can only be a Fragment.
我使用的是本教程中的代码,但不幸的是,每个“页面”只能有一个片段。
What i tried to do is to transform my Activities into Fragments (but i'm still getting some NullPointerException). 我试图做的是将我的活动转换为片段(但是我仍然遇到NullPointerException)。
Is it safe to use a Fragment instead of an Activity (in which i'm using : AlarmManager, OnCheckedChangeListener, PackageManager, AlertDialog...)? 使用Fragment而不是Activity是否安全(我在其中使用:AlarmManager,OnCheckedChangeListener,PackageManager,AlertDialog ...)? Is there a better way to solve my issue?
有没有更好的方法来解决我的问题?
Thank you. 谢谢。
====================================================================== ================================================== ====================
(UPDATE 1) (更新1)
Here is some code from the converted Fragment : 这是来自转换后的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;
}
I am getting a NullPointerException for usernameView.setText("")
我收到了
usernameView.setText("")
的NullPointerException
02-10 15:03:35.203 27866-27866/com.projectname E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
You can't use multiple activity in a single page. 您不能在单个页面中使用多个活动。
The only way is fragments,Fragments are build to support this feature. 唯一的方法是片段,构建片段以支持此功能。
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.
您可以在一个活动中组合多个片段以构建多窗格UI,并在多个活动中重用一个片段。 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).
您可以将片段视为活动的模块化部分,该片段具有自己的生命周期,接收其自己的输入事件,并且可以在活动运行时对其进行添加或删除(类似于“子活动”,您可以在不同的活动中重复使用)。
Converting Activity into fragment is very simple 将Activity转换成片段非常简单
Eg : 例如:
Activity - 活动 -
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Converted Fragment - 转换后的片段 -
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;
}
}
Most important 最重要的
Inside activity usually we are used to find view from xml using this method 在活动内部,通常我们使用这种方法从xml查找视图
TextView textView= (TextView) findViewById(R.id.helletxtview);
Inside fragments this will not work because findViewById not available in Fragment class,But use can use with the help of root View like this 在片段内部这将不起作用,因为在Fragment类中不存在findViewById,但是使用可以在像这样的根视图的帮助下使用
View view=inflater.inflate(R.layout.activity_main,null);
TextView textView= (TextView) view.findViewById(R.id.helletxtview);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.