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
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
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.