2017-05-24 150 views
-1

我在Android中導航抽屜時遇到了一個問題。Android Studio中導航抽屜的問題

我在空活動,我需要傳遞給抽屜式導航,我在 空活動代碼

final Button Login = (Button) findViewById(R.id.btnLogin); 
    Login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent new_from = new Intent(Login.this, MainActivity.class); 
      startActivity(new_from); 
     } 
    }); 

此代碼是正確的。

抽屜類是:

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

現在,在抽屜裏,問題是這條線

setSupportActionBar(toolbar); 

最後一行關閉我的申請。

PD:所有活動都在Manifest中。

感謝和快樂的日子

+1

把你的一切都在這裏代碼和堆棧跟蹤以及 –

+0

你可能有動作條錯誤,請使用NoActionBar主題爲同一活動 –

+0

哪裏是你的堆棧跟蹤?????什麼是錯誤?把你的主題也放在這裏 –

回答

0

既然你已添加工具欄並以編程方式將其設置爲操作欄,您需要更改樣式

去體現和編輯您的抽屜的活動標籤是這樣的:

<activity 
     android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
     android:name=".MainActivity"> 
</activity> 
+0

感謝你的回答,我的程序現在工作:D –

0

既然你沒有提供有關這個問題的所有信息,我會解決這個問題,假設你沒有設置抽屜式導航適當的條件下,最有可能你有問題與在設置活動和默認主題中的工具欄時發生衝突。

如果你想在你的MainActivity(setSupportActionBar(工具欄))已經寫的方式創建工具欄導航抽屜裏,你需要改變應用程序的主題。

你可以做到這一點清單文件:

android:theme="@android:style/Theme.NoTitleBar" 

你也可以改變,在RES /價值/ styles.xml:

通過自定義主題

<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
</style> 

<style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light"> 
    <item name = "android:windowActionBar">false</item> 
    <item name = "android:windowNoTitle">true</item> 
</style> 

或者只需更換主題(更好的方法是從清單此):

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

如果你想設置抽屜式導航在一些其他的方式,檢查這個鏈接,瞭解有關此主題的幫助:

StackOverflow - How to create Android Custom Striped Navigation Drawer
Android Developer
JournalDev
TeamTreeHouse