【问题标题】:Start a new Intent by setClassName with same package in android通过setClassName在android中使用相同的包启动一个新的Intent
【发布时间】:2018-06-09 23:51:26
【问题描述】:

我必须开始一个活动类,我知道如何开始一个活动,但问题是我的类名是动态的。我的意思是我们有 5 个活动名为 1. 活动 2. 通知 3.聊天 4. 留言 5.设置

我们还必须在 TabWidget 中显示上面的名字,但他的名字的顺序是来自 server 。因此,有时服务器在第一个索引处返回事件,有时通知在第一个索引处。 order 可以动态更改,它可能会返回其中的两个或三个,因此我们必须显示 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 有这样的数据

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>

但它正在崩溃并且 我的错误日志是

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?

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为您只是缺少一个 .以下行中为 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);
    

    【讨论】:

    • 我已经这样做了,但没有什么积极的仍然有同样的问题?
    • 这很奇怪,这是我假设的,基于您在 com.mcm.menuandnotificationEvents 中的错误缺少“。”在事件之前。尝试使用以下内容怎么样: Class> clazz = Class.forName("com.mcm.menuandnotification." + NameOfClass);意图.setClass(clazz);
    • 你的最后一个解决方案终于奏效了。但我仍然很想知道为什么上一个不起作用?
    • 我不确定为什么即使在第一个解决方案之后点仍然丢失,但我很高兴第二个解决方案有所帮助。 :)
    【解决方案2】:

    看看这个答案。

    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

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多