您好我是Android编程的新手。
我使用此链接创建了一个带有标签布局和可滑动视图的应用程序: source
我在活动文件下有1个活动文件TabMainActivity.java
和3个片段类。
我的TabMainActivity.java
引用此xml文件:
main_menu.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
我遇到的错误是当我尝试将数据从TabMainActivity.java
传递到MyLockerFragment.java
(片段文件)时。
TabMainActivity.java -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
String email = "tv.nguyen.2011@sis.smu.edu.sg";
Bundle bundle=new Bundle();
bundle.putString("email", email);
MyLockerFragment fragobj=new MyLockerFragment();
fragobj.setArguments(bundle);
}//end of of onCreate method
MyLockerFragment.java -
public class MyLockerFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylocker,
container, false);
Bundle bundle = this.getArguments();
String email=bundle.getString("email");
}
错误在此行:String email=bundle.getString("email");
我错过了一些基本的东西吗?
答案 0 :(得分:0)
在TabMainActivity.java中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
String email = "tv.nguyen.2011@sis.smu.edu.sg";
Bundle bundle=new Bundle();
bundle.putString("email", email);
MyLockerFragment fragobj=new MyLockerFragment();
fragobj.setArguments(bundle);
//You needed to add this line to the fragment Activity onCreate() method(othwewise the getArguments() would return null):
getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragobj).commit();
}//end of of onCreate method
在MyLockerFragment.java中:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String email = getArguments().getString("email");
View rootView = inflater.inflate(R.layout.fragment_mylocker,
container, false);
return rootView;
}