- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我调用 putString 到 SharedPreference 时,会出现问题。我的程序首先通过 TileService 运行,TileService 运行 ForegroundService,然后从 ForegroundService 打开一个 Activity,并在该 Activity 中调用 putString 到 SharedPreference。
如何让它完美运行?
平铺服务
...
@Override
public void onClick()
{
super.onClick();
Intent intent = new Intent(mContext,AdzanService.class);
if(mTileEnabled)
intent.setAction("STOP_SERVICE");
else
intent.setAction("START_SERVICE");
startForegroundService(intent);
mTileEnabled = !mTileEnabled;
}
...
服务
...
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if (intent.getAction().equals("STOP_SERVICE"))
{
stopForeground(true);
stopSelf();
}
...
Activity
...
private void save(){
mEditor.putString("setting_location",((EditText)findViewById(R.id.setting_location)).getText().toString());
mEditor.putString("setting_times",((RadioButton)findViewById(((RadioGroup)findViewById(R.id.setting_times)).getCheckedRadioButtonId())).getTag().toString());
mEditor.apply();
}
...
错误
java.lang.RuntimeException: Unable to start service in.blackant.adzan.AdzanService@873299c with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3722)
at android.app.ActivityThread.access$1600(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1686)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6693)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
at in.blackant.adzan.AdzanService.onStartCommand(AdzanService.java:97)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3703)
... 8 more
最佳答案
正如错误消息所述,当您调用 intent.getAction()
时,服务类中的 Intent 为 null
此处 Intent 可为空 according to the docs
提供给 Context.startService(Intent) 的 Intent,如给定。如果服务在其进程消失后重新启动,并且之前已返回除 START_STICKY_COMPATIBILITY 之外的任何内容,则该值可能为 null。
在检查 getAction()
之前添加空检查将修复崩溃,但您可能想了解服务重新启动的原因
关于java - 在 SharedPreference.Editor 上调用 putString 时,我的应用程序强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084858/
如何使用 putStr 和 map 输出字符串列表?我想做一些类似的事情: s=["test1","test2"] map putStr s 但是没有使用 monad 的经验,也不知道如何正确使用..
我正在尝试以这种格式打印出整数列表 [1] [2] [3] 所以我的函数接受一个整数列表并返回一个 IO () ,如下所示: import System.IO printVector :: [Inte
我有一个定义如下的 POJO 对象: public class MYPOJO implements Serializable { int myint; String mystring;
这个问题在这里已经有了答案: Wrong IO actions order using putStr and getLine (1 个回答) 关闭 5 年前。 当我想在读取 Haskell 中的输入
这个问题在这里已经有了答案: Wrong IO actions order using putStr and getLine (1 个回答) 关闭去年。 我是从 Learn You a Haskel
这个问题已经有答案了: IO happens out of order when using getLine and putStr (3 个回答) 已关闭 9 年前。 假设我们有一个简短的 haske
我最近开始使用 Android Studio 3.1.2 和 SDK 19 编写我真正的第一个 android 项目。 我目前正在为我的组件编写单元测试,并使用 Mockito 模拟依赖于 Andro
我正在尝试使用新方法 putString 上传 base64 图像。我已经更新到 3.3,但 TS 说 Property putString does not exist on type Refere
我需要将 2 个变量从一个 Activity 传递到另一个 Activity 。 第一个 Activity 我有以下内容: @Override public boolean onContextItem
我有一个简单的程序,只需从用户处获取一个字符串和一个 key ,并使用凯撒密码函数对该字符串进行加密。该函数本身可以工作,所以我不会展示其来源。问题是,当编译器编译程序时,它将允许我输入所有 getL
我正在 Android Studio 1.1.0 中处理 Android 项目,我无法将消息传递给具有 Bundle 类的处理程序。 好吧,这是我的代码 ... Bundle mUIMessageBu
var storageRef = firebase.storage().ref(); var ref = storageRef.child('nothing.txt'); ref.putString(
每次我的应用程序启动时,我都会加载相同的基本共享首选项。 用户可以选择添加自己的条目,效果很好。 我想确保首选项文件不只是添加大量重复项,我假设如果它找到确切的条目,它将跳过 putString 命令
我的 ionic 2 应用程序中有一个简单的功能,可以将文件上传到我的 firebase 存储服务器。它从相机中获取图像的 base64 编码字符串,但当我不尝试强制使用 content-type 时
本文整理了Java中com.zx.zxutils.util.ZXSharedPrefUtil.putString()方法的一些代码示例,展示了ZXSharedPrefUtil.putString()的
假设您使用 putStr 的组合提示用户输入和 getLine : main = do putStrLn "A line with line termination" -- printed c
我是 Haskell 的新手,我想知道如何在 Haskell 中打印两个函数的结果,我会在 C++ 中这样做: cout Int square x = x * x fib :: Int -> Int
代码如下: package com.androidbook.triviaquiz; import android.app.Activity; import android.content.Shared
有了这个文件: main = do putStr "Input: " s <- getLine putStr s 它在 GHCi 中完成了我想要的操作,即将提示放在与提示相同的行上,然后允
我设置此线程是为了阐明 firebase 存储 putString 方法在 React-native 中是否或不是否有效. 根据我的搜索,目前无法使用 put 方法将文件或 Blob 类型上传到 Fi
我是一名优秀的程序员,十分优秀!