Android Studio导航抽屉项目打开网站



我使用了基本的"导航抽屉活动";在android Studio中创建新项目。碎片工作得很好,我已经设法添加了新的碎片和一切。然而,在抽屉菜单中引导你到某个网站的项目不起作用。我已经尝试了不同的方法与公共布尔onNavigationItemSelected(MenuItem item){}和什么都没有发生,当我按下项目。

它看起来像这样:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.nav_moodle) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
return true;
}

正如你所看到的,我没有为片段工作做任何事情,因为它们已经工作得很好了,但是当我按下按钮"moodle"时,什么也没有发生。

我做错了什么?

这是我的MainActivity:

package com.example.ustudy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;
import com.example.ustudy.ui.home.HomeFragment;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.ustudy.databinding.ActivityMainBinding;
import org.jetbrains.annotations.NotNull;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());


//required for the top bar to be clickable
setSupportActionBar(binding.appBarMain.toolbar);
//button in the bottom right corner
binding.appBarMain.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 = binding.drawerLayout;
//Navigationview for the drawer (implemented in "activity_main.xml")
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
//each screen mentioned in the columns below (R.id.xy) will show the hamburger menu in the top left corner
//on screens, not included in the columns, there will be a "back arrow" instead of the ham. menu
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_nachricht, R.id.nav_lerngruppe, R.id.nav_lehrveranstaltung)
.setDrawerLayout(drawer)
.build();
//controls what happens when one of the items in the hamburger menu is clicked on
//go to "mobile_navigation.xml" to add screens to go to
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
//required to set up the hamburger menu icon and the back-arrow icon on the top bar(without it
//the user had to slide the side of the screen to open the menu)
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
//enables the items in the hamburger menu to go to a certain screen after clicking on them
NavigationUI.setupWithNavController(navigationView, navController);


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu (three dots on the right); this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
//enables the usage of the back-arrow on every screen
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}

@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_moodle) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
return true;
}
}

如果你需要更多,请告诉我!

最新更新