我从MyListFragment中的args获取null, 无法弄清楚原因。
基本上我要做的是从地图获取位置 并将旧片段替换为具有更新位置的新片段,
MyListFragmnet是NavigationDrawerActivity
中的“默认”片段// MyListFragment.java
public static MyListFragment newInstance() {
Bundle args = new Bundle();
return newInstance(args);
}
public static MyListFragment newInstance(Bundle args) {
MyListFragment fragment = new MyListFragment();
fragment.setArguments(args);
return fragment;
}
public MyListFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.setHasOptionsMenu(true);
Bundle args = getArguments();
searchQuery = args.getString(SEARCH_QUERY_ARG, ""); //NULL ERROR!!
final MyLocation checkLocation = (MyLocation) args.getSerializable(LOCATION_ARG);
if (!searchQuery.isEmpty()) {
activity.setTitle("\""+searchQuery+"\"");
}
setLocation(checkLocation);
............
private void setLocation(final MyLocation checkLocation) {
if (checkLocation != null) {
// If no location is set and there is a check
location = new Location("");
location.setLatitude(checkLocation.latitude);
location.setLongitude(checkLocation.longitude);
} else {
// If no location was provided, get the current one
location = LocationHelper.getInstance().getLocation(context);
}
}
// NavigationDrawerActivity.java
public class NavigationDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
private NavigationView navLayout;
//Location
private MyLocation checkLocation;
private boolean active;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_navigation);
setupReferences();
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
try {
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setSubtitle(getString(R.string.subtitle));
actionBar.setDisplayShowTitleEnabled(true);
} catch (Exception ignored) {
}
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
toggle = new ActionBarDrawerToggle(this,
drawerLayout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
navLayout= (NavigationView)findViewById(R.id.navLayout);
navLayout.setNavigationItemSelectedListener(this);
active = false;
checkLocation = (MyLocation) getIntent().getSerializableExtra(MyListFragment.LOCATION_ARG);
handleIntent(getIntent());
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.frameLayout, new MyListFragment());
tx.commit();
}
@Override
public boolean onNavigationItemSelected(MenuItem menuItem){
switch (menuItem.getItemId()){
case R.id.navigation_item_1:
break;
case R.id.navigation_item_2:
break;
case R.id.navigation_item_3:
break;
}
return false;
}
@Override
protected void onNewIntent(Intent intent) {
if (active) {
// Only handles the Intent again if the Activity was already active
handleIntent(intent);
}
}
@Override
public void startActivity(Intent intent) {
super.startActivity(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
intent.putExtra(MyListFragment.LOCATION_ARG, checkLocation);
}
active = true;
}
private void handleIntent(Intent intent) {
Bundle args = new Bundle();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String searchQuery = intent.getStringExtra(SearchManager.QUERY);
args.putString(MyListFragment.SEARCH_QUERY_ARG, searchQuery);
args.putSerializable(MyListFragment.LOCATION_ARG, checkLocation);
} else if (MyListFragment.ACTION_CHECK.equals(intent.getAction())) {
checkLocation = (MyLocation) intent.getSerializableExtra(LocationActivity.EXTRA_LOCATION);
args.putSerializable(MyListFragment.LOCATION_ARG, checkLocation);
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout, MyListFragment.newInstance(args)).commit();
}
}
请帮忙!感谢。
答案 0 :(得分:2)
您没有使用MyListFragment.newInstance()
,只是致电new MyListFragment()
这一行
tx.replace(R.id.frameLayout, new MyListFragment());
应该是
tx.replace(R.id.frameLayout, MyListFragment.newInstance());
答案 1 :(得分:1)
下面:
...
tx.replace(R.id.frameLayout, new MyListFragment());
...
在创建MyListFragment
Fragment的对象时不传递任何包。
通过调用newInstance(Bundle args)
静态方法来获取Fragment对象。
Bundle bundle = new Bundle();
bundle.putString(SEARCH_QUERY_ARG, "data_here");
...
tx.replace(R.id.frameLayout, new MyListFragment.newInstance(bundle));
...