HarmonyOS 鸿蒙Next 关于首选项读不了put方法的问题
HarmonyOS 鸿蒙Next 关于首选项读不了put方法的问题
关于首选项无法读取put方法的问题
按文档 arkui-数据管理-首选项进行测试
直接写在入口ability里面
import hilog from '@ohos.hilog';
import { Ability } from '@ohos.application.Ability'
import { Window } from '@ohos.window'
import data_preferences from '@ohos.data.preferences';
let context = null;
let preferences = null;
export default class EntryAbility extends Ability {
onCreate(want, launchParam) {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
//初始化首选项
let promise = data_preferences.getPreferences(this.context,'sdjsdj33');
promise.then((pref)=>{
preferences = pref;
}).catch((err)=>{
console.info('dataBase init promise catch:' + err);
})
//添加数据
let putPromise = preferences.put('hello','world');
putPromise.then(()=>{
console.info('dataBase put promise succeed:');
}).catch((err)=>{
console.info('dataBase put promise catch:' + err);
})
}
onDestroy() {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: Window.WindowStage) {
// Main window is created, set main page for this ability
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR);
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
context = this.context;
}
onWindowStageDestroy() {
// Main window is destroyed, release UI related resources
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground() {
// Ability has brought to foreground
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground() {
// Ability has back to background
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}
然后就报了一个错误
这是原log,我实在时搞不懂,首选项的文档里面说的明明白白,api 9 he stage模式适用,难度我这不是?模拟器是远程的p50,写的清清楚楚api 9啊。。。。,新建项目选的也是api 9和stage啊。。。兄弟们救救我。
Device info:OpenHarmony 2.0 Canary
Build info:OpenHarmony 3.2.6.3
Module name:com.brejce.managermony
Version:1.0.0
Pid:1201
Uid:20040003
Reason:TypeError
Error message:Cannot read property put of null
Stacktrace:
Cannot get SourceMap info, dump raw stack:
更多关于HarmonyOS 鸿蒙Next 关于首选项读不了put方法的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
根据日志看,你得设备用的时2.x,编译环境是3.x啊
更多关于HarmonyOS 鸿蒙Next 关于首选项读不了put方法的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
啊,我虽然看到2.x和3.x了但是怎么也没想到,这远程模拟器p50api9咋就是2.x版本啊啊啊啊啊啊,那我刚升级了3.x的nova8可以用罗?我现在用的官方示例-保存字体大小那个来改的可以用。
针对HarmonyOS 鸿蒙Next中关于首选项读不了put方法的问题,这通常涉及到数据存储和读取的机制。在鸿蒙系统中,如果你遇到无法正常使用put方法存储数据到首选项(类似于Android中的SharedPreferences),可能的原因及解决方案包括:
-
权限问题:确保你的应用已经获得了存储数据的权限。虽然鸿蒙系统对于权限管理有自己的机制,但检查权限是第一步。
-
API使用错误:鸿蒙系统的API可能与Android有所不同,确保你使用的是鸿蒙系统提供的正确API来进行数据存储。检查你的代码是否按照鸿蒙的文档正确使用了相关的数据存储类和方法。
-
数据类型问题:确保你尝试存储的数据类型是被支持的。某些复杂或自定义的数据类型可能需要特殊处理才能存储。
-
代码错误:检查代码中是否有逻辑错误或异常处理不当的情况,这可能导致数据存储失败。
-
系统Bug:如果以上都不是问题,可能是鸿蒙系统本身的Bug。这种情况下,可以尝试更新系统或查找是否有相关的系统补丁。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,