2

I am using collapsible action bar. While trying to start new activity got error: Cannot resolve constructor 'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class)'

Please help me on this:

Can we use start new activity from navigation drawer menu item click ?

Or

We have only option to replace and show fragments from navigation drawer on menu item click

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

if yes, then how can I replace tab activity and viewpager and set action bar height ?

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;
        }
    });
userdks
  • 51
  • 1
  • 1
  • 6
  • Now I'm able to start a new activity. Is this practice good if I want to keep the same navigation drawer in another activity ? how can I achieve to keep navigation drawer same for all other activites too ? – userdks Oct 11 '15 at 12:22

2 Answers2

10

According to Android Docs to start new activity you have to do:

1) Add this line in your class:

import android.content.Intent;

2) Add new activity to manifest, looks like this:

 <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) Start activity with startActivity() method, like this:

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

3a) If your class extends Activity you can pass this instead of getApplicationContext():

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

3b) If your class extends Fragment you must use getActivity() to pass Context:

Intent newAct = new Intent(getActivity(), YourActivityName.class);
startActivity(newAct);
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • I have added to that in android manifest. Intent is not resolved when used inside onNavigationItemSelected(menuItem menuItem) – userdks Oct 11 '15 at 11:12
  • Have you add: import android.content.Intent; ?? – Michele Lacorte Oct 11 '15 at 11:14
  • 1
    Use getApplicationContext() instead of this?...because you use an anonymous class... – Michele Lacorte Oct 11 '15 at 11:20
  • using getApplicationContext() gives error on runtime: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? – userdks Oct 11 '15 at 11:36
  • Excuse where you "run" the code of the navigation view ?? you could post the whole class ? – Michele Lacorte Oct 11 '15 at 11:40
  • However add this line: newAct.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before startActivity – Michele Lacorte Oct 11 '15 at 11:43
  • Is this practice good if I want to keep the same navigation drawer in another activity ? How can I achieve to keep navigation drawer same for all other activities too ? – userdks Oct 11 '15 at 12:26
  • You have to use fragment for keep same navigation drawer in another page :D – Michele Lacorte Oct 11 '15 at 12:27
  • how can I replace tab layout and view pager using: fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit(); If I make a container view after this line "" in my activity_main and put viewpager inside that. Then how can I turn off tab layout ? – userdks Oct 11 '15 at 12:34
  • check this: http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager or http://stackoverflow.com/questions/13625048/android-fragments-with-tabs-and-viewpager or https://guides.codepath.com/android/sliding-tabs-with-pagerslidingtabstrip may help you – Michele Lacorte Oct 11 '15 at 12:48
2

You wrote as:

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

Cannot resolve constructor 'Intent(anonymous android.support.design.widget.NavigationView.OnNavigationItemSelectedListener, java.lang.Class)'

this at there is an instance of NavigationView.OnNavigationItemSelectedListener.

But the first constructor argument of Intent should be Context.

So set Context to a final variable outside of the NavigationView.OnNavigationItemSelectedListener:

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

Then pass the context to the constructor of Intent.

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

It will be work.

Or, I would not use anonymous object. I would implement NavigationView.OnNavigationItemSelectedListener in the MainActivity itself.

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

For this way you can use this as an instance of Context.

hata
  • 11,633
  • 6
  • 46
  • 69