Java 如何在Android中创建侧导航栏
我正在尝试在我的应用程序中创建侧导航栏。我的主要活动代码是:Java 如何在Android中创建侧导航栏,java,android,Java,Android,我正在尝试在我的应用程序中创建侧导航栏。我的主要活动代码是: package com.thechamp.ait; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcom
package com.thechamp.ait;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.accessibility.AccessibilityRecord;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import java.security.MessageDigest;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
navigationView = findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
if(timeOfDay >= 0 && timeOfDay < 12){
Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 12 && timeOfDay < 16){
Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 16 && timeOfDay < 21){
Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 21 && timeOfDay < 24){
Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}
drawerLayout = findViewById(R.id.drawer_layout);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected( MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AboutCollege()).commit();
break;
case R.id.nav_Earnmoney:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new EarnMoney()).commit();
break;
case R.id.nav_Feedback:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Feedback()).commit();
break;
case R.id.nav_notice:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NoticeBoard()).commit();
break;
case R.id.nav_study:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new StudyMaterial()).commit();
break;
case R.id.nav_support:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Support()).commit();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}}
您没有关闭内容框
LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<!-- this is your content frame - first child of DrawerLayout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/background_pic"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- ends in here -->
<!-- this is your side bar attached to "start" (layout_gravity param) -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@null"
app:menu="@menu/drawer_menu" />
<!-- you may add more side bars with different layout_gravity -->
</androidx.drawerlayout.widget.DrawerLayout>
这在我的情况下不起作用,它使我的应用程序强制停止显示您的异常日志您是指logcat还是错误logwell,错误日志在logcat中,不是吗…?我已在我的问题中添加了我的错误日志onNavigationItemSelected调用了吗?是。请检查我的所有编码。并帮助我更正代码,然后您可以尝试调试onNavigationItemSelected方法并检查发生了什么。您正在单击哪个项目?看起来这个问题的答案是“在应用程序中使用有效的XML”。
2020-10-14 18:11:26.782 363-363/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-10-14 18:11:26.782 363-363/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-10-14 18:11:39.348 16712-16712/? E/oid.apps.photo: Not starting debugger since process cannot load the jdwp agent.
2020-10-14 18:11:39.358 16723-16723/? E/android.youtub: Not starting debugger since process cannot load the jdwp agent.
2020-10-14 18:11:39.427 16723-16723/? E/YouTube: flushBinderConnectionCallbacks is unverified on SDK 30
2020-10-14 18:11:39.437 191-197/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-10-14 18:11:39.439 191-197/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-10-14 18:11:39.447 506-523/? E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket
2020-10-14 18:11:39.518 408-425/? E/installd: Couldn't opendir /data/app/vmdl1992756199.tmp: No such file or directory
2020-10-14 18:11:39.518 408-425/? E/installd: Failed to delete /data/app/vmdl1992756199.tmp: No such file or directory
2020-10-14 18:11:39.704 1003-1793/? E/ActivityThread: Failed to find provider info for com.google.android.apps.wellbeing.api
2020-10-14 18:11:39.896 16723-16788/? E/GEL_DELAYED_EVENT_DEBUG: Failed delayed event dispatch, no dispatchers.
2020-10-14 18:11:44.711 1003-1793/? E/ActivityThread: Failed to find provider info for com.google.android.apps.wellbeing.api
2020-10-14 18:11:59.668 506-523/? E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket
2020-10-14 18:11:59.673 10616-15676/? E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-10-14 18:11:59.727 1003-1793/? E/ActivityThread: Failed to find provider info for com.google.android.apps.wellbeing.api
2020-10-14 18:12:09.692 506-523/? E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket
2020-10-14 18:12:09.693 506-523/? E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket
2020-10-14 18:12:12.577 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:12.577 506-650/? E/ClipboardService: Denying clipboard access to com.android.chrome, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:12.577 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:12.826 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:12.826 506-650/? E/ClipboardService: Denying clipboard access to com.android.chrome, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:12.826 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:13.001 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:13.001 506-650/? E/ClipboardService: Denying clipboard access to com.android.chrome, application is not in focus nor is it a system service for user 0
2020-10-14 18:12:13.001 506-650/? E/ClipboardService: Denying clipboard access to com.google.android.gms, application is not in focus nor is it a system service for user 0
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<!-- this is your content frame - first child of DrawerLayout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/background_pic"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- ends in here -->
<!-- this is your side bar attached to "start" (layout_gravity param) -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@null"
app:menu="@menu/drawer_menu" />
<!-- you may add more side bars with different layout_gravity -->
</androidx.drawerlayout.widget.DrawerLayout>