我在将参数传递给主要活动的片段时遇到问题。 getArguments方法返回null但同时片段显示我试图传递的参数但位于不同的位置。我确定它与我正在替换替换方法的容器有关。
我的主要活动:
public class MainActivity extends AppCompatActivity {
private final String FRAGMENT_TAG = "fragment_tag";
public static final String USER_NAME = "userName";
public static final String USER_PASSWORD = "userPassword";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
User user = getIntent().getParcelableExtra(LoginActivityFragment.SERVER_MESSAGE);
Bundle bundle = new Bundle();
bundle.putString(USER_NAME, user.getUser_name());
bundle.putString(USER_PASSWORD, user.getUser_password());
if (user != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MainActivityFragment fragment = (MainActivityFragment) getSupportFragmentManager()
.findFragmentByTag(FRAGMENT_TAG);
if (fragment != null) {
ft.replace(android.R.id.content, fragment);
ft.commit();
} else {
MainActivityFragment newFragment = new MainActivityFragment();
ft.replace(android.R.id.content, newFragment, FRAGMENT_TAG);
newFragment.setArguments(bundle);
ft.commit();
}
}
}
}
我的MainActivityFragment:
public class MainActivityFragment extends Fragment {
private final String ERROR = "Error";
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView username = (TextView) rootView.findViewById(R.id.username_test);
TextView password = (TextView) rootView.findViewById(R.id.pass_test);
Bundle bundle = getArguments();
try {
username.setText(bundle.getString(MainActivity.USER_NAME));
password.setText(bundle.getString(MainActivity.USER_PASSWORD));
} catch (Exception e) {
Log.e(ERROR, "Error: " + e.getMessage());
}
return rootView;
}
}
MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.msc.ebla.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</SearchView>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main布局
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.msc.ebla.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
MainActivityFragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.msc.ebla.MainActivityFragment"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/username_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="user"/>
<TextView
android:id="@+id/pass_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pass"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
以下是我将testName和testPass作为参数传递时模拟器的屏幕截图:
答案 0 :(得分:2)
看起来你正在加载两个Fragment
s - 一个静态,一个动态。您在Fragment
中声明的静态xml
以及此getArguments()
中的静态null
将始终返回Fragment
。但动态<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.msc.ebla.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
不应该。
我建议你只使用动态片段。替换:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
使用
MainActivity
在您的android.R.id.content
中,将所有对R.id.fragment_container
的引用替换为newInstance()
修改强>
顺便说一句,我建议使用Fragment
方法将参数传递给MainActivityFragment
,因为它更可靠。将此方法声明添加到public static MainActivityFragment newInstance(String userName, String userPassword) {
Bundle args = new Bundle();
args.putString(USER_NAME_KEY, userName);
args.putString(USER_PASSWORD_KEY, userPassword);
MainActivityFragment frag = new MainActivityFragment();
frag.setArguments(args);
return frag;
}
:
new MainActivityFragment()
而不是使用MainActivityFragment.newInstance("user_name", "user_password)
创建新实例,而是使用selectOptions = [{id: 1, name: 'name1'}, {id: 2, name: 'name2'}, {id: 3, name: 'name3'}];
。还有更多信息here
答案 1 :(得分:0)
你应该设置参数 BEFORE 调用replace()和commit()。因此,将代码更改为以下内容:
MainActivityFragment newFragment = new MainActivityFragment();
newFragment.setArguments(bundle);
ft.replace(android.R.id.content, newFragment, FRAGMENT_TAG);
ft.commit();
您的content_main布局与AppBar布局处于同一级别。你必须把它放在AppBar布局中。**像这样:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</SearchView>
</android.support.v7.widget.Toolbar>
<include layout="@layout/content_main" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>