所以我只想在我的菜单中显示搜索图标(或SearchView),如果当前选择了搜索导航项,但是如果我尝试在onNavigationItemSelected中执行setVisible,我会收到错误说:
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference
onNavigationItemSelected:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_search) {
Context context = getApplicationContext();
Toast.makeText(context, "Debug: Search Navigation Item Selected", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_content);
Log.d("FCM", "Instance ID: " + FirebaseInstanceId.getInstance().getToken());
Toolbar toolbar = findViewById(R.id.toolbarS);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(1).setChecked(true);
MenuItem search = findViewById(R.id.menuSearch);
search.setVisible(false);
} else if (id == R.id.nav_watchlist) {
Context context = getApplicationContext();
Toast.makeText(context, "Debug: Watchlist Navigation Item Selected", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_login) {
this.startActivity(new Intent(this, AuthUiActivity.class));
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
try {
if (currentFirebaseUser != null) {
Log.d("AUTH", currentFirebaseUser.getUid());
Log.d("AUTH", currentFirebaseUser.getDisplayName());
Log.d("AUTH", currentFirebaseUser.getEmail());
Log.d("AUTH", currentFirebaseUser.getPhoneNumber());
Log.d("AUTH", currentFirebaseUser.getProviderId());
}
} catch (NullPointerException e) {
Log.d("AUTH", "At least one Firebase parameter is undefined: " + e.toString());
}
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
menu_search.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="@+id/menuSearch"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
myapp:actionViewClass="android.widget.SearchView"
myapp:showAsAction="collapseActionView|ifRoom">
</item>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/language"
app:showAsAction="never" />
</menu>
但是,setVisible方法适用于onCreateOptionsMenu。
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem menuItem = menu.findItem(R.id.menuSearch);
menuuu = menu;
final SearchView searchView = (SearchView)menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
ListView lvR = findViewById(R.id.listViewResultsS);
lvR.setAdapter(null);
try {
postNewSeriesByName(getApplicationContext(), s);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error with tvdb API.", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
//handle query changes here, helpful for auto complete stuff
//make textboxes go away when you start typing
try {
TextView textview3 = findViewById(R.id.textView3);
textview3.setVisibility(View.GONE);
} catch (NullPointerException e) {
Log.e("SEARCH", "wrong view you fuck (3)");
}
try {
TextView textview2 = findViewById(R.id.textView2);
textview2.setVisibility(View.GONE);
} catch (NullPointerException e) {
Log.e("SEARCH", "wrong view you fuck (2)");
}
return false;
}
});
menuItem.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
感谢您提供的任何帮助。
答案 0 :(得分:0)
在一个Activity中多次调用setContentView不是一个好主意。 这里有一个解决问题的答案
Android: is using setContentView multiple times bad while changing layouts?
在我看来,每次要显示其他屏幕时,您都可以使用片段而不是调用setContentView