2016-06-19 37 views
0

我目前正在嘗試編寫一個小應用程序,現在我正嘗試在我的應用程序中使用導航抽屜。這不起作用,因爲我在drawer.addDrawerListener(toggle);獲得了NPE。就我所能說的,其他一切似乎都沒有問題,應用程序正常工作直到添加抽屜。 Thx提前。NPE在添加抽屜監聽器

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; 
    } 
} 

回答

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

+0

好的,thx工作!但是,我的錯是什麼,我想了解我的未來應用程序的失敗! –

+0

@school_guy如果這個答案對你有幫助,你可以接受答案。 – Ironman

+0

你的回答很快Stackoverflow阻止我檢查 –