- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在 StackOverFlow 上浏览了很多答案,但无法正常工作。我想在抽屉导航项目点击时切换 fragment 。我在 Android Studio 1.5 中使用默认的抽屉导航。
我关注了this回答,我已经确定我已经创建了所有 fragment 。问题是每当我单击任何菜单项时,应用程序都会停止工作。这是我到目前为止所拥有的。
主 Activity .java
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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;
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);
displayView(R.id.twitter);
}
@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) {
// 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.
displayView(item.getItemId());
return true;
}
public void displayView(int viewId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.facebook:
fragment = new FacebookFragment();
title = "Facebook";
break;
case R.id.twitter:
fragment = new TwitterFragment();
title = "Twitter";
break;
case R.id.googleplus:
fragment = new GooglePlusFragment();
title = "Google Plus";
break;
case R.id.settings:
fragment = new SettingsFragment();
title = "Settings";
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
FacebookFragment.java
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FacebookFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FacebookFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class FacebookFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public FacebookFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FacebookFragment.
*/
// TODO: Rename and change types and number of parameters
public static FacebookFragment newInstance(String param1, String param2) {
FacebookFragment fragment = new FacebookFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_facebook, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
我已将 FrameLayout 添加到 content_main.xml 文件中,正如我提到的问题中所建议的那样。感谢您的帮助。
最佳答案
根据你的 logcat,错误是:
com.internetwarz.sploon.MainActivity@3caae0bc 必须实现 OnFragmentInteractionListener
我认为发生错误是因为您的父 Activity (MainActivity.java) 没有实现此方法:OnFragmentInteractionListener
通过评论可以看到:
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
所以,解决方案是:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
FacebookFragment.OnFragmentInteractionListener {
....
// Add the method below to MainActivity.java
private void onFragmentInteraction(Uri uri) {
// Do something or just return
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
}
关于android - 使用 onNavigationItemSelected() 在抽屉导航中切换 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35642055/
当单击任何菜单项时,没有响应,他们应该导航到目标 fragment @Override public boolean onNavigationItemSelected(@NonNull
我想在有人点击导航 View 时调用 onNavigationItemSelected。代码不调用 onNavigationItemSelected 方法: public boolean onNav
我正在为 android 3+ 开发 在我的操作栏中,我有一个下拉列表(请参阅 how to hide/unhide the actionbar list on android 3? 了解我想要的下拉
我在我的应用程序中实现了一个导航 View ,它是由 AndroidStudio 自动创建的。当我创建一个新项目时,我选择了 NavigationDrawer Activity,菜单项看起来没问题,但
当我编写 mNavigationView.setNavigationItemSelectedListener(this); 时,应用程序崩溃在代码中如果未添加,则会删除导航栏中该项目的功能。我应该在后
我正在为一个包含抽屉的工程设计项目开发 Android 应用。我的一位小组成员选择使用 android.support.design.widget.NavigationView API 来执行此操作,
我正在使用 ActionBarSherlock 来显示微调器: ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionB
所以我在 StackOverFlow 上浏览了很多答案,但无法正常工作。我想在抽屉导航项目点击时切换 fragment 。我在 Android Studio 1.5 中使用默认的抽屉导航。 我关注了t
请有人帮我处理抽屉导航中的 fragment ,出于某种原因我无法让它们工作,但所有代码看起来都正确。 Here是源代码的链接。 最佳答案 使用这段代码: navigationView = (Navi
我有一个包含 3 个菜单项的抽屉导航。在我的 MainActivity.java 中,我设置了 onNavigationItemSelected() 监听器来处理单击的导航项。单击时,将显示一个自定义
我正在使用 Google+ 登录,这会导致 Navigation.activity。我已经按照 Google 开发人员的说明进行操作,但我只采用了登录方法(成功),但现在我卡在了 navigation
我想在应用程序启动时选择抽屉导航中的特定项目。我在 visual studio 默认模板中添加了 navigationView.SetCheckedItem(Resource.Id.nav_galle
我在 Kotlin 中制作了抽屉菜单,我想使用这个菜单项。在 java 中,我调用了 onNavigationItemSelected 方法,但是当我想在 Kotlin 中使用它时,它没有出现。这是我
我有一个 BottomNavigationView在我与 onNavigationItemSelected 的 Activity 中听众。 我有两个问题: 是否 setSelectedItemId方法
我有一个 ViewPager 托管在一个 fragment 中,该 fragment 托管两个 ListFragments,这两个 ListFragments 由从 SQLite 查询返回的 Curs
我想在下面的 Activity 中的操作栏中使用微调器是 onCreateOptionsMenu:我使用 this tutorial实现这种方法。我的问题是当 Activity 是午餐时,onNavi
我将 ActionBar 与下拉菜单一起使用,onNavigationItemSelected() 在 Activity 创建后立即被调用,因此第一个项目被调用。我的下拉菜单的第一项是主页,与使用 a
我在 DrawerLayout 中使用 NavigationView,我不确定我是否忘记了什么,但只是在我单击它时隐藏了 Navigation。 这是我的代码: 主要 Activity .java:
我已经实现了选项卡 View 以及水平滑动 View (以更改选项卡),导航模式作为应用程序的列表。我正在使用 ActionBarSherlock 和 TabPageIndicator。更改标签后,我
我从 Android Studio 3.5 模板创建了一个新的抽屉导航 Activity ,但即使我正在监听 NavigationItemSelected,菜单项也无法单击。 public class
我是一名优秀的程序员,十分优秀!