我必须开始一个活动课,我知道如何开始活动,但问题是我的班级名称是动态的。我的意思是我们有5个名为的活动 1.事件 2.通知 3.聊天 4.留言 5.设置
我们还必须在TabWidget中显示以上名称,但他的名字顺序来自服务器。因此,有时服务器在第一个索引返回事件,有时通知是第一个索引。订单可以动态更改,它可能会返回两个或三个,并根据我们必须显示TabWidget。
这意味着如果服务器返回带有两个字符串的数组,那么我们只有两个选项卡,如果它返回三个,那么我们有三个选项卡。
目前我正在使用此方法动态添加标签
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);
}
}
我的menuListArray有datalike这个
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\]]
并且在清单中我宣布了所有活动
<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>
但它正在崩溃 我的错误logcat是
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?
答案 0 :(得分:0)
看看这个答案。
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
答案 1 :(得分:0)
我认为你只是错过了一个。以下行中Intent的指定字符串中的字符:
intent = new Intent().setClassName("com.mcm.menuandnotification",
"com.mcm.menuandnotification" + NameOfClass);
将其更改为:
intent = new Intent().setClassName("com.mcm.menuandnotification",
"com.mcm.menuandnotification." + NameOfClass);
编辑:
如果这不起作用,请尝试以下方法:
Class<?> clazz = Class.forName("com.mcm.menuandnotification." + NameOfClass);
intent.setClass(this, clazz);