Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:12:05+00:00 2026-06-08T20:12:05+00:00

I am trying to link different buttons to different activities. so in effect creating

  • 0

I am trying to link different buttons to different activities. so in effect creating a navigation menu.

I am trying to use less code so am trying to create one function i can pass values to in order to dynamically identify the buttons id and give it a link to the activity.

Here is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/nav1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/nav1" />

    <Button
        android:id="@+id/nav2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/nav1"
        android:text="@string/nav2" />

</RelativeLayout>

Here is my Java:

package com.example.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Nav extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);

        String nav1 = "nav1";
        String nav2 = "nav2";

        String MainActivity = "MainActivity";
        String SQLite = "SQLite";

        navButton(nav1, MainActivity);
        navButton(nav2, SQLite);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_nav, menu);
        return true;
    }

    private void navButton(String buttonId, String activityName){
        String bId = buttonId;
        final Class<?> aName = Class.forName(activityName);
        int resId = getResources().getIdentifier(bId, "id", getPackageName());
        Button b = (Button) findViewById(resId);
        OnClickListener onClickListener = new OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Nav.this, aName);
                startActivity(i);
            }};
        b.setOnClickListener(onClickListener);
    }
}

I am getting an error on line:

final Class<?> aName = Class.forName(activityName);

And it says:

Unhandled exception type ClassNotFoundException

Or quite simply whats the best way to do this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:12:07+00:00Added an answer on June 8, 2026 at 8:12 pm

    Any time I use Class.forName I pass in a qualified name, e.g. “mypackage.myclass”. I’m not sure if it works with an unqualified name.

    In any case, why pass in the string and then do a forName? Why not change the signature to take a Class object and just pass in MainActivity.class, etc? Then you’d catch any missing classes or spelling errors at compile time instead of run time.

    Update in response to comment

    public class Nav extends Activity { 
    
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_nav); 
    
        navButton("nav1", MainActivity.class); 
        navButton("nav2", SQLite.class); 
    } 
    
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_nav, menu); 
        return true; 
    } 
    
    private void navButton(String buttonId, Class aName){ 
        String bId = buttonId; 
        // aName now passed in so doesn't need to be looked up
        ... etc ...
    } 
    } 
    

    I think the above should compile and run. I haven’t tried it but I’ve done similar things. Depending on how your packages are set up, you may need to import MainActivity and SQLite, or fully quality them, in order to take the .class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to use persits upload code to upload pictures to 2 different
I'm trying to load different Google Maps depending on what link they click. Something
I'm trying to link to an external stylesheet using this code: <?php include(/homepages/9/myusername/htdocs/Mobile_Detect.php); $detect
I am trying to create a dropdown menu that when you click it will
We just had an interesting experience in trying to link a set of code
I am trying to create a simple GUI. I have a menu bar that
I was trying to use IB in a slightly different way that I am
I am trying to implement multiple Facebook share buttons in one page to share
Im trying to link my program to the shared library. Im using a makefile
I am trying to link to an external link using the anchor tag like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.