首页 \ 问答 \ 自定义全局应用程序类与“android.app.Application无法转换”中断(Custom global Application class breaks with “android.app.Application cannot be cast to”)

自定义全局应用程序类与“android.app.Application无法转换”中断(Custom global Application class breaks with “android.app.Application cannot be cast to”)

我有一个这样的自定义全局类

import android.app.Application;

public class MyApp extends Application {

    public String MainAct;

    public String getMainAct() {
        return MainAct;
    }

    public void setMainAct(String mainAct) {
        MainAct = mainAct;
    }
}

我想通过其onCreate方法中的另一个Activity保留此类中的字符串。

    String local = "myLocalVariable";
    ((MyApp) getApplication()).setMainAct(local); //breaks here!!!
    String name = ((MyApp) getApplication()).getMainAct();

它在标记行上发生错误: Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.yyy.global.MyApp

我已经检查了代码五次,我无法找到任何错误。 谁能告诉我错误在哪里?

谢谢


I have a custom global class which looks like this

import android.app.Application;

public class MyApp extends Application {

    public String MainAct;

    public String getMainAct() {
        return MainAct;
    }

    public void setMainAct(String mainAct) {
        MainAct = mainAct;
    }
}

I want to preserve a string in this class via another Activity in its onCreate method.

    String local = "myLocalVariable";
    ((MyApp) getApplication()).setMainAct(local); //breaks here!!!
    String name = ((MyApp) getApplication()).getMainAct();

It breaks on the marked line with error: Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.yyy.global.MyApp

I've checked the code for five times and I cannot find error anywhere. Can anyone tell me where the error is?!

Thanks


原文:https://stackoverflow.com/questions/10607392
更新时间:2024-04-02 12:04

最满意答案

-Contains操作符不进行子串比较,匹配必须在完整的字符串上,并用于搜索集合。

从您链接到的文档:

- 包含
说明:遏制操作员。 告知参考值的集合是否包含单个测试值。

在您提供的示例中,您正在使用仅包含一个字符串项的集合。

如果您阅读链接的文档,您将看到一个演示此行为的示例:

例子:

PS C:\> "abc", "def" -Contains "def"
True

PS C:\> "Windows", "PowerShell" -Contains "Shell"
False  #Not an exact match

我想你想要的是 - -Match运算符:

"12-18" -Match "-"

哪个返回True


The -Contains operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.

From the documentation you linked to:

-Contains Description: Containment operator. Tells whether a collection of reference values includes a single test value.

In the example you provided you're working with a collection containing just one string item.

If you read the documentation you linked to you'll see an example that demonstrates this behaviour:

Examples:

PS C:\> "abc", "def" -Contains "def"
True

PS C:\> "Windows", "PowerShell" -Contains "Shell"
False  #Not an exact match

I think what you want is the -Match operator:

"12-18" -Match "-"

Which returns True.

Important: As pointed out in the comments and in the linked documentation, it should be noted that the -Match operator uses regular expressions to perform text matching.

相关问答

更多
  • PowerShell没有 - 但.NET [string]类型具有Insert()方法: PS C:\> "abc".Insert(0,"xyz") xyzabc 你仍然无法捷径分配,它会变成: $targetPath = $targetPath.Insert(0,'Microsoft.PowerShell.Core\FileSystem::') 或者,创建一个为您执行此操作的函数: function Prepend-StringVariable { param( [string ...
  • 两个运算符都会在您的场景中生成一个数组,但是逗号运算符不会插入数组,因此您将获得一个数组数组,而+运算符会将第二个数组的元素追加到第一个数组的元素。 当您以JSON格式输出数组时,它会变得更清晰: PS C:\> $a = 1, 2 PS C:\> $b = 3, 4 PS C:\> ($a, $b) | ConvertTo-Json [ { "value": [ 1, 2 ...
  • 我使用-an运算符更新了每个Arnavion的代码,并且它按预期工作。 foreach($acc in get-SPmanagedAccount) { $accNamewithDomain = $acc.UserName $accname = $accNamewithDomain.Split("\")[1] if(($accName -ne "svc-reader-tst") -and ($accName -ne "svc-writer-tst")) { # ...
  • 它返回false,因为filename与pattern不匹配。 一个更好的正则表达式是: $pattern = '^\d+_\d{8}_\d{14}\.pdf$' $filename = '123456_12345678_12345678901234.pdf' $filename -match '\d{6}_\d{8}_\d{14}\.pdf.*' 说明 ^在字符串的开头 \d+匹配至少一位数(如你所说的数字位数未知) _匹配下划线 \d{8}匹配8位数字 _匹配下划线 \d{14}匹配14位数 \. 匹 ...
  • 你可以简化它 if ($user_sam -and $user_case) { ... } 因为空字符串会强制为$false (因此, $null也是如此)。 You can simplify it to if ($user_sam -and $user_case) { ... } because empty strings coerce to $false (and so does $null, for that matter).
  • -Contains操作符不进行子串比较,匹配必须在完整的字符串上,并用于搜索集合。 从您链接到的文档: - 包含 说明:遏制操作员。 告知参考值的集合是否包含单个测试值。 在您提供的示例中,您正在使用仅包含一个字符串项的集合。 如果您阅读链接的文档,您将看到一个演示此行为的示例: 例子: PS C:\> "abc", "def" -Contains "def" True PS C:\> "Windows", "PowerShell" -Contains "Shell" False #Not an exa ...
  • -contains运算符不是字符串运算符,而是集合包含运算符: 'a','b','c' -contains 'b' # correct use of -contains against collection 来自about_Comparison_Operators帮助主题 : Type Operator Description Containment -contains Returns true when reference value contained in a c ...
  • 尝试 If ($User -match "administrator" -or $User -match "kahuna") 您的-or运算符不会将前一个运算符的值绑定在一起。 你需要在-or之后指定第二个条件运算符。 Try If ($User -match "administrator" -or $User -match "kahuna") Your -or operator doesn't tie the values of the previous operator together. You ...
  • 除非在它们之间放置-OR,否则不能使用相同条件“OperatingsystemVersion”的两倍。 如果为同一计算机对象搜索5.1和6.1,则不会得到任何结果。 import-module ac* Get-ADComputer -filter { OperatingSystemVersion -Like '*6.1*' -OR OperatingSystemVersion -Like '*5.1*' -and Enabled -eq "true"} -SearchBase 'OU=MIComputer ...
  • pip freeze | Out-File -Encoding UTF8 requirements.txt 或者你可以试试 pip freeze > iconv -f UTF-8 -t ISO-8859-1 in.txt > out.txt 你可以阅读有关iconv的内容 pip freeze | Out-File -Encoding UTF8 requirements.txt or you can try pip freeze > iconv -f UTF-8 -t ISO-8859-1 in.tx ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)