0

screenshot of the navigation drawer

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.richelle.hci">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="Monitoring Health"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SettingsActivity"
                android:label="@string/title_activity_settings"></activity>
        </application>
    </manifest>

nav_header_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/side_nav_bar"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <ImageButton
            android:id="@+id/profile_image"
            android:layout_width="83dp"
            android:layout_height="75dp"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:scaleType="fitCenter"
            android:onClick="onClick"
            app:srcCompat="@drawable/profile" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:text="Android Studio"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android.studio@android.com" />

    </LinearLayout>

MainActivity.java

    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);
    //        ActionBar actionBar = getSupportActionBar();
    //        actionBar.setDisplayShowHomeEnabled(true);
    //        actionBar.setIcon(R.mipmap.ic_launcher);
            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.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            ImageButton ib = (ImageButton)navigationView.findViewById(R.id.profile_image);
            ib.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    Intent intentLoadNewActivity = new Intent(MainActivity.this, Profile.class);
                    startActivity(intentLoadNewActivity);
                }
            });
        }

I am trying to use the image button in the navigation draw to open up a new activity page. The image button was added in the nav_header_main.xml and the code to call the new activity was placed in the MainActivity.java. However, the application will force close once I run it. Can anybody help me out with this?

  • You have to find the `ImageButton` in the `NavigationView`'s header, specifically; e.g., `...navigationView.getHeaderView(0).findViewById(R.id.profile_image);`. – Mike M. Nov 23 '17 at 01:38
  • @MikeM. I have added the screenshot of the navigation drawer. The image button is the profile picture. I can use this: navigationView.getHeaderView(0).findViewById(R.id.profile‌​_image); as well for that? – Richelle Chia Nov 23 '17 at 06:14
  • If `nav_header_main` is set as the `headerLayout` attribute on the `` element, then yes, that's how you get the `profile‌​_image` `ImageButton` – and any other `View` inside the header – in the `onCreate()` method. – Mike M. Nov 23 '17 at 06:28
  • @MikeM. now the app can be open once it is open however it force close again when i clicked onto the image button of the profile picture – Richelle Chia Nov 23 '17 at 07:57
  • That's a different problem. Look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Nov 23 '17 at 08:33
  • it says "android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.richelle.hci/com.example.richelle.hci.Profile}; have you declared this activity in your AndroidManifest.xml?" So I have to add something in the AndroidManifest.xml? – Richelle Chia Nov 24 '17 at 14:36
  • Yes, all `Activity` classes have to be listed in the manifest. https://stackoverflow.com/questions/3433778 – Mike M. Nov 24 '17 at 23:27
  • @MikeM. I solved it already thanks so much – Richelle Chia Nov 26 '17 at 04:18

0 Answers0