gpt4 book ai didi

java - 无法从 Activity 到 fragment 获取 bundle 值

转载 作者:行者123 更新时间:2023-12-02 09:03:11 28 4
gpt4 key购买 nike

请帮忙,我刚刚学习了 android,我正在尝试使用 bundle 将数据从我的主 Activity 发送到 fragment 。这是 MainActivity 的代码

@Override
public boolean onQueryTextChange(String newText) {

if (newText.length()>0){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("query", newText);
SearchMovieFragment searchMovieFragment = new SearchMovieFragment();
searchMovieFragment.setArguments(bundle);
transaction.replace(R.id.frame_container, searchMovieFragment);
transaction.commit();
}

这是我的 fragment 中的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

String query = getArguments().getString("query");
MoviesViewModel moviesViewModel = ViewModelProviders.of(this).get(MoviesViewModel.class);
moviesViewModel.setSearchedMovie(query);
moviesViewModel.getMovie().observe(this, getListMovies);

return inflater.inflate(R.layout.fragment_movies, container, false);
}

此行发生错误String query = getArguments().getString("query");错误是

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

我该如何解决这个问题?

这是我的主要 Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBarGradiant(this);
setContentView(R.layout.activity_main);

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);

ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.color_primary_gradient));
}

getSupportActionBar().setElevation(0);
}

protected void setStatusBarGradiant(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
Drawable background = activity.getResources().getDrawable(R.drawable.color_primary_gradient);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
window.setBackgroundDrawable(background);
}
}

private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);

if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}

final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

if (searchManager != null) {
final SearchView searchView = (SearchView) (menu.findItem(R.id.search)).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return true;
}

@Override
public boolean onQueryTextChange(String newText) {

if (newText.length()>0){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("query", newText);
SearchMovieFragment searchMovieFragment = new SearchMovieFragment();
searchMovieFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,searchMovieFragment).commit();
}
else {
loadFragment(new SearchMovieFragment());
}
return true;
}
});
}
return true;
}

}

和我的 SearchMovieFragment

public class SearchMovieFragment extends Fragment {
private RecyclerView rvMovies;
private ArrayList<Movie> list = new ArrayList<>();
private CardViewMovieAdapter adapter;
private ProgressBar progressBar;
private String query;

public SearchMovieFragment() {
// Required empty public constructor
}

private Observer<ArrayList<Movie>> getListMovies = new Observer<ArrayList<Movie>>() {
@Override
public void onChanged(ArrayList<Movie> movies) {
if (movies != null) {
adapter.setData(movies);
}
else {
Toast.makeText(getContext(), R.string.no_result, Toast.LENGTH_LONG).show();
}
showLoading(false);
}
};

private static final String ARG_PARAM1 = "query";
public static SearchMovieFragment newInstance(String param1) {
SearchMovieFragment fragment = new SearchMovieFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

query = getArguments().getString("query");
return inflater.inflate(R.layout.fragment_movies, container, false);
}


public void onViewCreated(View view, Bundle savedInstanceState) {
rvMovies = view.findViewById(R.id.rv_movies);
rvMovies.setHasFixedSize(true);
progressBar = view.findViewById(R.id.progressBar);

MoviesViewModel moviesViewModel = ViewModelProviders.of(this).get(MoviesViewModel.class);
moviesViewModel.setSearchedMovie(query);
moviesViewModel.getMovie().observe(this, getListMovies);

showLoading(true);
showRecyclerCardView();
}

private void showRecyclerCardView() {
rvMovies.setLayoutManager(new LinearLayoutManager(this.getContext()));
adapter = new CardViewMovieAdapter(list);
rvMovies.setAdapter(adapter);
}

}

最佳答案

你能尝试删除这个吗?

private static final String ARG_PARAM1 = "query";
public static SearchMovieFragment newInstance(String param1) {
SearchMovieFragment fragment = new SearchMovieFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}

希望它能与您合作

关于java - 无法从 Activity 到 fragment 获取 bundle 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018085/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com