Error : Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
I Am Making a Search Button that will send the search Query to fragment And From there it loads the URL with a search query
for eg if I search pen in the search bar. the query will send using bundle and will load in webview in another fragment.
Activity code
firstFragmentBtn = findViewById(R.id.fragment1btn);
secondFragmentBtn = findViewById(R.id.fragment2btn);
searchView = findViewById(R.id.edit_query);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("sendData", searchView.getQuery().toString());
fragment1 fragobj = new fragment1();
fragobj.setArguments(bundle);
replaceFragment(new fragment1());
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
firstFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new fragment1());
}
});
secondFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new fragment2());
}
});
fragment code
WebView webView = view.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
Bundle bundle = getArguments();
// error in this line
String message = bundle.getString("SendData"); ????
webView.loadUrl("https://google.com/search?q=" message);
return view;
CodePudding user response:
In your fragment inside onCreateView()
if(getArguments().getString("SendData") != null)
{
String message = getArguments().getString("SendData");
.
.
.
CodePudding user response:
in putString ("sendData") whereas in getString it is "SendData" put same in both either sendData or SendData