自定义全局应用程序类与“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 itsonCreate
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
最满意答案
-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 ...