我一直得到null而不是预期的字符串。我已经通过getSharedPreferences应用程序的上下文和正确的密钥。我将上传XML文件。我不知道这是怎么回事。
import React, {Component} from "react";
class RGBGuesser extends Component {
constructor(){
super();
this.state = {
correctCount: 0,
displayCorrect: 0,
colors: "",
chosenResult: "",
chosenCorrect: 0,
}
}
componentDidMount = () => {
this.startGame();
}
initialGameState = () => {
this.setState({
colors: this.displayRandom(6)
})
}
restart = () => {
this.initialGameState();
this.setState({
chosenResult: "",
chosenCorrect: 0,
displayCorrect: 0
})
}
pickSquare = () => {
let colorRan = Math.floor(Math.random() * this.state.colors.length);
return this.state.colors[colorRan]
}
displayRandom = amountSquares => {
const colorArr = [];
for(let i = 0; i < amountSquares; i++){
colorArr.push(this.chooseRandom());
}
return colorArr;
}
chooseRandom = () => {
let rColor = Math.floor(Math.random() * 256);
let gColor = Math.floor(Math.random() * 256);
let bColor = Math.floor(Math.random() * 256);
return `rgb(${rColor}, ${gColor}, ${bColor})`;
}
chooseSquare = () => {
//where i would want to do the logic of clicking the square and comparing it with the rgb color displayed on screen
}
startGame = () => {
this.initialGameState();
this.restart();
}
render(){
let correctColor = this.pickSquare();
return(
<div>
<h1 id="header">RGB Color Guesser</h1>
<h3 id="mainColor">{correctColor}</h3>
<h3 id="result"></h3>
<h3 id="showCorrect">Number Correct: <span id="correctCount">0</span></h3>
<button id="startOver" onClick={this.restart}>Start Over</button>
<div id="colorGrid">
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[0]}}></div>
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[1]}}></div>
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[2]}}></div>
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[3]}}></div>
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[4]}}></div>
<div className="coloredSquare" onClick={this.chooseSquare} style={{backgroundColor: this.state.colors[5]}}></div>
</div>
</div>
)
}
}
export default RGBGuesser;
在线
java.lang.NullPointerException: println needs a message
首选项文件中没有显示任何内容,但是键是正确的,momentData是有效的SharedPreferences对象。至少根据调试器。
这是XML,下面是代码。
Log.d("MomentPrefTimeStamp", momentData.getString("caption", null));
为什么我不能使用正确的密钥和上下文来获取数据?
-
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="time_stamp">1529509324</string>
<string name="img_uri">content://com.example.android.fileprovider/my_images/JPEG_20180620_114201_1480896410651074556.jpg</string>
<string name="caption">Captionas</string>
</map>
答案 0 :(得分:1)
如果要通过字符串技巧和文件目录获取SharedPreferences,请确保从文件名中删除扩展名。我不知道为什么它仍然给我一个SharedPreference对象而不是一个空指针异常,但这就是问题所在。
1234242_JPEG.xml必须为1234242_JPEG才能获取正确的SharedPreference文件。 下面是修复程序。
String name = title.substring(0, title.lastIndexOf('.'));
Log.d("MomentName", name);
SharedPreferences momentData = this.getSharedPreferences(name, this.MODE_PRIVATE);