2

I have to start a activity class , i know how to start an activity but the problem is that my class name is dynamic. I mean we have 5 activity named 1. Events 2. Notification 3. Chat 4. Message 5. Settings

we also have to show this above name in TabWidget but the order of his name is coming from server . Hence sometime server return Events at first index, sometimes notification is at first index. order can be changed dynamically and it may return two or three of them and according to that we have to show the TabWidget.

its means if the server returning array with two string then we have only two tab if it return three then we have three tab.

currently I am using this method to add tab dynamically

private void setTabDynimacaaly() {
String NameOfClass;
for (int i = 0; i < menuListArray.size(); i++) {
    NameOfClass = menuListArray.get(i).get(4).toString().trim();
    intent = new Intent().setClassName("com.mcm.menuandnotification",
            "com.mcm.menuandnotification" + NameOfClass);

    spec = tabHost.newTabSpec(NameOfClass).setIndicator("")
            .setContent(intent);

    // Add intent to tab
    tabHost.addTab(spec);
  }
}

my menuListArray has datalike this

06-02 07:18:06.732: E/MY APP MENU DATA(19739): [[38, 206, 5, MyChurchMateApp, Events, 1, \Images\MyChurchMateApp\ThemeImages\], [38, 206, 4, MyChurchMateApp, Notifications, 5, \Images\MyChurchMateApp\ThemeImages\], [38, 206, 7, MyChurchMateApp, Settings, 100, \Images\MyChurchMateApp\ThemeImages\]]

and in manifest i have declared all the activity

<activity
  android:name=".menuandnotification.TabBar"
  android:screenOrientation="portrait" >
  </activity>
  <activity
  android:name=".menuandnotification.Events"
 android:screenOrientation="portrait" >
</activity>
   <activity
    android:name=".menuandnotification.Notifications"
   android:screenOrientation="portrait" >
  </activity>
  <activity
     android:name=".menuandnotification.Message"
    android:screenOrientation="portrait" >
</activity>
<activity
  android:name=".menuandnotification.Chat"
 android:screenOrientation="portrait" >
</activity>

<activity
  android:name=".menuandnotification.Settings"
 android:screenOrientation="portrait" >
</activity>

but it is getting crashed and my error logcat is

06-02 07:18:07.662: E/AndroidRuntime(19739): java.lang.RuntimeException: Unable to  
start activity ComponentInfo{com.mcm/com.mcm.menuandnotification.TabBar}: 
android.content.ActivityNotFoundException: Unable to find explicit activity class  
{com.mcm.menuandnotification/com.mcm.menuandnotificationEvents}; have you declared 
this activity in your AndroidManifest.xml?
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nirmal
  • 939
  • 10
  • 24

2 Answers2

0

Have a look at this answer.

Intent intent  = new Intent(Intent.ACTION_MAIN).addCategory(
intent.CATEGORY_LAUNCHER).setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass));

Cannot start new Intent by setClassName with different package in Android

Community
  • 1
  • 1
MjZac
  • 3,476
  • 1
  • 17
  • 28
0

I think you are just missing a . character in your specified string for the Intent in the following line:

intent = new Intent().setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification" + NameOfClass);

Change it to:

intent = new Intent().setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass);

EDIT:

If that didn't work, how about trying the following:

Class<?> clazz = Class.forName("com.mcm.menuandnotification." + NameOfClass); 
intent.setClass(this, clazz);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I have already done that but nothing positive still having the same problem? – Nirmal Jun 02 '14 at 12:15
  • That is strange, it is what I assumed based on that your error in com.mcm.menuandnotificationEvents is lacking the '.' before Events. How about trying to use the following: Class> clazz = Class.forName("com.mcm.menuandnotification." + NameOfClass); intent.setClass(clazz); – EpicPandaForce Jun 02 '14 at 12:23
  • finally your last solution is working. but i am still eager to know why the previous one is not working? – Nirmal Jun 02 '14 at 12:46
  • I'm not sure why the dot was missing even after the first solution, but I'm glad the second solution helped. :) – EpicPandaForce Jun 02 '14 at 12:48