【问题标题】:Unable to extend Main Activity with CustomHashMap无法使用 CustomHashMap 扩展主活动
【发布时间】:2015-02-27 14:51:02
【问题描述】:

参考:Android:- How to Make dynamic HashMap Accessible in whole application? 我已经设置了一个 CustomHashMap(只是 3 个 LinkedHashMaps 的集合),以便我可以从 2 个活动中访问它,但我是 无法从 SecondActivity 访问它!删除 Manifest.xml 中的红色错误的唯一方法是设置以下行。如果我 使用 android:name="com.example.intents.CustomHashMap" (按照在线示例)我收到一条错误消息,说 'is not an enclosure 类'。

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher_icon"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme"
    android:name="com.example.intents.MainActivity$CustomHashMap">  //--- The system added the $ parts here.
    <activity
    ....
 protected CustomHashMap thisMap;
 ....
 thisMap = (MainActivity.CustomHashMap)getApplication();

在 MainActivity 中,我可以执行 thisMap.getKeyValue(4) 之类的操作,但从 SecondActivity 中只返回 null。我觉得我好像 这里缺少一些非常简单的东西。有什么想法吗?

【问题讨论】:

  • 你为什么不把它变成一个包含这些哈希图的静态类呢?您的错误是告诉您您的课程嵌套在第一个活动中。请发布此活动。

标签: android


【解决方案1】:

见这里android:name

为应用程序实现的应用程序子类的完全限定名称

意味着需要传递扩展 Application 的类名,而不是任何其他组件名,如 Activity、Service、...

在应用程序中添加Application 类见以下教程:

Global Variable Or Application Context Variable - Android Example

【讨论】:

  • 全部修复。你是对的,因为我在错误的类中使用了 CustomHashMap。我创建了一个新的 Activity (CustomHashMap),一切正常。
猜你喜欢
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多