【问题标题】:How to start an activity on material navigation drawer menu item clicked如何在单击的材质导航抽屉菜单项上启动活动
【发布时间】:2015-10-11 10:57:01
【问题描述】:

我正在使用可折叠的操作栏。 尝试开始新活动时出错:无法解析构造函数“Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class)”

请帮我解决这个问题:

我们可以使用从导航抽屉菜单项单击开始新活动吗?

或者

我们只能选择在菜单项单击时从导航抽屉中替换和显示片段

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

如果是,那么如何替换选项卡活动和 viewpager 并设置操作栏高度?

activity_main.xml

<android.support.v4.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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.app.activities.MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:minHeight="?attr/actionBarSize"

            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorHeight="4dp"
            app:tabGravity="fill"
            app:tabMode="scrollable"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    </android.support.design.widget.AppBarLayout>

         <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:src="@drawable/ic_plus"

        app:borderWidth="0dp"
        app:fabSize="normal" />

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/nav_item_icon_tint_color"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/navigation_drawer_items" />

MainActivity.java

navigation = (NavigationView) findViewById(R.id.navigation);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            drawerLayout.closeDrawers();
            switch (id){
                case R.id.home:
                    //do something here
                    //open new activity
                    /*unable to start activity*/
                    //Intent intent = new Intent(this, NewActivity.class);
                    /* error on above line shown: Cannot resolve constructor 'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class<com.test.app.activities.RouteActivity>)'*/
                    break;
                case R.id.logout:
                   //add navigation drawer item onclick method here
                    break;

            }
            return false;
        }
    });

【问题讨论】:

  • 现在我可以开始一项新活动了。如果我想在另一个活动中保留相同的导航抽屉,这种做法好吗?我怎样才能使所有其他活动的导航抽屉也保持相同?

标签: android material-design


【解决方案1】:

根据Android Docs开始新的活动你必须做的:

1) 在你的类中添加这一行:

import android.content.Intent;

2) 向 manifest 添加新 Activity,如下所示:

 <activity
        android:name=".YourActivityName"
        android:label="YourActivityName">
        <intent-filter>
            <action android:name="package.name.YOURACTIVITYNAME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

3) 使用startActivity() 方法启动活动,如下所示:

Intent newAct = new Intent(getApplicationContext(), YourActivityName.class);
startActivity(newAct);

3a) 如果您的课程扩展 Activity,您可以通过 this 而不是 getApplicationContext()

Intent newAct = new Intent(this, YourActivityName.class);
startActivity(newAct);

3b) 如果你的类扩展了Fragment,你必须使用getActivity() 来传递上下文:

Intent newAct = new Intent(getActivity(), YourActivityName.class);
startActivity(newAct);

【讨论】:

  • 我已在 android manifest 中添加了该内容。在 onNavigationItemSelected(menuItem menuItem) 中使用 Intent 时未解析
  • 有没有添加:import android.content.Intent; ??
  • 使用 getApplicationContext() 代替这个?...因为您使用匿名类...
  • 使用 getApplicationContext() 会在运行时出错:android.util.AndroidRuntimeException: 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?
  • 请问您在哪里“运行”导航视图的代码??你可以发布整个课程吗?
【解决方案2】:

你写的是:

/*unable to start activity*/
Intent intent = new Intent(this, NewActivity.class);

无法解析构造函数'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class)'

this 有一个NavigationView.OnNavigationItemSelectedListener 的实例。

但是Intent的第一个构造函数参数应该是Context

因此将 Context 设置为 NavigationView.OnNavigationItemSelectedListener 之外的最终变量:

final Context context = this;
navigation = (NavigationView) findViewById(R.id.navigation);
navigation.setNavigationItemSelectedListener(
    new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        ...

然后将上下文传递给Intent的构造函数。

Intent intent = new Intent(context, NewActivity.class);

会成功的。

或者,我不会使用匿名对象。我会在 MainActivity 本身中实现NavigationView.OnNavigationItemSelectedListener

MainActivity extends Activity
        implements NavigationView.OnNavigationItemSelectedListener {
    ...
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
       ...
       Intent intent = new Intent(this, NewActivity.class);

为此,您可以使用this 作为Context 的实例。

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多