我的问题是Bundle bundle=getArgument();
为空。
我尝试ActivitySerializable activitySerializableTest=(ActivitySerializable)bundle.getSerializable("activitySerializable");
也是空的。
当我的布局是片段时,如何从Object获取值?
非常感谢任何帮助。
My Serializable calss是这样的:
public class ActivitySerializable implements Serializable {
private String EDUTITLE;
public String getEDUTITLE() {
return EDUTITLE;
}
public void setEDUTITLE(String EDUTITLE) {
this.EDUTITLE = EDUTITLE;
}
}
我的A片段尝试设置如下值:
public class ActivityListDomesticFragment extends Fragment{
.......
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ActivitySerializable activitySerializable=new ActivitySerializable();
activitySerializable.setEDUTITLE("Value");
//set the Bundle
ActivityContents activityContentsFragment=new ActivityContents();//i want to get value from this fragment
Bundle serializableBundle=new Bundle();
serializableBundle.putSerializable("activitySerializable",activitySerializable);
activityContentsFragment.setArguments(serializableBundle);//send bundle
switchFragment(activityHomePage);//switch to next Fragment
}
//change fragment function
private void switchFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
transaction.addToBackStack(null);
transaction.commit();
}
我的B片段获得如下值:
public class ActivityContents extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_contents_fragment, container, false);
Bundle bundle = getArguments();
// it shows my activitySerializableTest is on a null object reference
ActivitySerializable activitySerializableTest=(ActivitySerializable)bundle.getSerializable("activitySerializable");
String test=activitySerializableTest.getEDUIN();
activityCtEduin.setText(test);
if (bundle != null) {
String EDUTITLE=activitySerializable.getEDUTITLE();
activityCtTitle.setText(EDUTITLE);
else{
//bundle is null
}
return view;
}
}
通过单击MainActivity抽屉项目更改为ActivityContents。 这是它的布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<include layout="@layout/toolbar"/>
//fragment change over here-------------------------------------
<FrameLayout
android:id="@+id/mainFrame"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
我关于更改片段的主要活动:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
.......
@Override
public void onClick(View view) {
if (id == R.id.activitiesContents) {
//here is change to ActivityContents fragment
switchFragment(ActivityContents.newInstance());
drawerLayout.closeDrawer(GravityCompat.START);
}
}
答案 0 :(得分:1)
Fragment
不应将getArguments
作为字段的初始化程序。从生命周期回调中调用它,例如onCreateView
。在创建对象时,尚未设置参数,因此当前代码将始终返回null
。
您还应该考虑使用Parcelable
代替Serializable
,因为它更有效。此外,如果您的参数是基元或字符串,则无需使用Serializable
或Parcelable
,Bundle
将直接处理基本类型和字符串。
答案 1 :(得分:0)
我找到一个解决方案来保存我的问题,它是关于单身人士:
我将它用于我的Book类,如下所示:
public static Book book=null;
public static Book getBook(){
if (book==null){
book=new Book();
return book;
}else {
return book;
}
}
在A:
Book book=Book.getBook();
book.setName("Jerry");
在C:
Book book=Book.getBook();
textView.setText(book.getName());
然后我的问题结束了,我必须放弃Serializable。