我目前正在尝试编写一个小应用程序,现在我正在尝试在我的应用程序中使用导航抽屉。这不适用于我在drawer.addDrawerListener(toggle);
获得NPE的原因。据我所知,其他所有内容似乎都很好,应用程序正常工作,直到添加抽屉。 Thx提前。
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import de.nocompany.gotthold.gw2companion.Tools.GwApiAccess;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawer.addDrawerListener(toggle);
navigationView.setNavigationItemSelectedListener(this);
ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
View navHeader = navigationView.getHeaderView(0);
if (networkInfo != null && networkInfo.isConnected()) {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_online);
} else {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_offline);
}
toggle.syncState();
GwApiAccess gwApiAccess = new GwApiAccess(this,"https://api.guildwars2.com/v2/items/30704?lang=de");
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = drawer.isDrawerOpen(GravityCompat.START);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//int id = item.getItemId();
//noinspection SimplifiableIfStatement
//if (id == R.id.action_settings) {
// return true;
//}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
Intent intent = new Intent(this,profile.class);
startActivity(intent);
} else if (id == R.id.nav_itemDB) {
Intent intent = new Intent(this,items.class);
startActivity(intent);
} else if (id == R.id.nav_guilds) {
Intent intent = new Intent(this,guilds.class);
startActivity(intent);
} else if (id == R.id.nav_maps) {
Intent intent = new Intent(this,map.class);
startActivity (intent);
} else if (id == R.id.settings) {
Intent intent = new Intent(this,SettingsActivity.class);
startActivity (intent);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:1)
首先更改此部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
到这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
由于您之前没有将布局设置为Actiivty
,因此您必须访问它内部元素,以便Studio无法找到它并且它会为您提供NullpointerExeception
。