在清单文件中做出一些声明。我尝试这样做,但不断出现一些错误。我的应用运行正常,但在选择菜单项时,我的应用停止。
安卓清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinsharma.nav">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我也知道我将不得不使用 Intent 方法来开始我的新活动,我再次这样做了,但它一直有同样的问题。
主要活动.java
package com.example.sachinsharma.nav
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.app.FragmentTransaction;
import android.util.Log;
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 static android.R.attr.value;
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);
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);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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 onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void displaySelectedScreen(int id){
Fragment fragment = null;
switch(id) {
case R.id.nav_dashboard:
fragment = new FragDashboard();
break;
case R.id.nav_attendance:
fragment = new FragAttendance();
break;
// case R.id.nav_homework:
// break;
// case R.id.nav_exams:
// break;
// case R.id.nav_fee:
// break;
// case R.id.nav_notifications:
// break;
// case R.id.nav_tracking:
// break;
// case R.id.nav_profile:
// break;
}
if(fragment != null){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.commit();
//this transaction does not replace layouts completely, it just overlaps and the rest
//of content_main layout can still be seen
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
displaySelectedScreen(id);
return true;
}
}
新活动.java
package com.example.sachinsharma.nav;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
/**
* Created by sachinsharma on 4/4/17.
*/
public class NewActivity extends MainActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
}
以下是我抽屉里的菜单项。
action_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_dashboard"
android:icon="@mipmap/ic_dashboard_black_24dp"
android:title="Dashboard" />
<item
android:id="@+id/nav_attendance"
android:icon="@mipmap/ic_watch_later_black_24dp"
android:title="Attendance" />
<item
android:id="@+id/nav_homework"
android:icon="@mipmap/ic_announcement_black_24dp"
android:title="Homework" />
<item
android:id="@+id/nav_exams"
android:icon="@mipmap/ic_assignment_black_24dp"
android:title="Exams" />
<item
android:id="@+id/nav_fee"
android:icon="@drawable/rupee"
android:title="Fee" />
<item
android:id="@+id/nav_notifications"
android:icon="@mipmap/ic_notifications_active_black_24dp"
android:title="Notifications" />
<item
android:id="@+id/nav_tracking"
android:icon="@mipmap/ic_directions_bus_black_24dp"
android:title="Tracking" />
<item
android:id="@+id/nav_profile"
android:icon="@mipmap/ic_account_circle_black_24dp"
android:title="Profile" />
</menu>
问题是你应该使用片段。您正在做的是打开另一个没有导航抽屉的活动和the fact is you can have only one navigation drawer per activity but multiple fragments per activity and not multiple activities per activity
。
尝试使用片段,一切都会正常工作,而且应用程序中有很多活动会使它滞后,用户很可能会删除您的应用程序。
还有什么让我知道的,for the code just create new project in android studio and select navigation drawer activity over there and create your project after that read the code you will get the concept of fragments
.
祝你好运!
- 在
AndroidManifest.xml
中声明NewActivity
- 不要忘记为
NewActivity
应用主题AppTheme.NoActionBar
。
按如下方式更新您的AndroidMenifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinsharma.nav">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
希望这会有所帮助~