質問編集履歴
3
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -147,3 +147,13 @@
|
|
147
147
|
editor.commit();
|
148
148
|
|
149
149
|
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
```Java
|
154
|
+
|
155
|
+
MainActivity main = new MainActivity();
|
156
|
+
|
157
|
+
main.loadFile();
|
158
|
+
|
159
|
+
```
|
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,6 +4,146 @@
|
|
4
4
|
|
5
5
|
```java
|
6
6
|
|
7
|
+
Realm realm = Realm.getDefaultInstance();
|
8
|
+
|
9
|
+
//リセット
|
10
|
+
|
11
|
+
RealmQuery<DataModel> query = realm.where(DataModel.class);
|
12
|
+
|
13
|
+
RealmResults<DataModel> result = query.findAll();
|
14
|
+
|
15
|
+
realm.beginTransaction();
|
16
|
+
|
17
|
+
result.deleteAllFromRealm();
|
18
|
+
|
19
|
+
realm.commitTransaction();
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
InputStream is = null;
|
24
|
+
|
25
|
+
BufferedReader br = null;
|
26
|
+
|
27
|
+
int times = 1;
|
28
|
+
|
29
|
+
try {
|
30
|
+
|
31
|
+
try {
|
32
|
+
|
33
|
+
is = this.getAssets().open("data.txt");
|
34
|
+
|
35
|
+
br = new BufferedReader(new InputStreamReader(is));
|
36
|
+
|
37
|
+
String str;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
String wordEnTemp = null;
|
42
|
+
|
43
|
+
String wordJpTemp = null;
|
44
|
+
|
45
|
+
String sentenceEnTemp = null;
|
46
|
+
|
47
|
+
String sentenceJpTemp = null;
|
48
|
+
|
49
|
+
int indexTemp = 0;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
while ((str = br.readLine()) != null) {
|
54
|
+
|
55
|
+
switch (times%4){
|
56
|
+
|
57
|
+
case 1:
|
58
|
+
|
59
|
+
//単語
|
60
|
+
|
61
|
+
wordEnTemp = str;
|
62
|
+
|
63
|
+
break;
|
64
|
+
|
65
|
+
case 2:
|
66
|
+
|
67
|
+
//和訳
|
68
|
+
|
69
|
+
wordJpTemp = str;
|
70
|
+
|
71
|
+
break;
|
72
|
+
|
73
|
+
case 3:
|
74
|
+
|
75
|
+
//例文
|
76
|
+
|
77
|
+
sentenceEnTemp = str;
|
78
|
+
|
79
|
+
break;
|
80
|
+
|
81
|
+
case 0:
|
82
|
+
|
83
|
+
//和訳
|
84
|
+
|
85
|
+
sentenceJpTemp = str;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
//データ保存
|
90
|
+
|
91
|
+
realm.beginTransaction();
|
92
|
+
|
93
|
+
DataModel model=realm.createObject(DataModel.class);
|
94
|
+
|
95
|
+
model.setIndex(indexTemp);
|
96
|
+
|
97
|
+
model.setWordEn(wordEnTemp);
|
98
|
+
|
99
|
+
model.setWordJp(wordJpTemp);
|
100
|
+
|
101
|
+
model.setSentenceEn(sentenceEnTemp);
|
102
|
+
|
103
|
+
model.setSentenceJp(sentenceJpTemp);
|
104
|
+
|
105
|
+
model.setProgressWord(0);
|
106
|
+
|
107
|
+
model.setProgressSpelling(0);
|
108
|
+
|
109
|
+
model.setTimeWord(0);
|
110
|
+
|
111
|
+
model.setTimeSpelling(0);
|
112
|
+
|
113
|
+
realm.commitTransaction();
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
indexTemp++;
|
118
|
+
|
119
|
+
break;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
times++;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
} finally {
|
128
|
+
|
129
|
+
if (is != null) is.close();
|
130
|
+
|
131
|
+
if (br != null) br.close();
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
} catch (Exception e){
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
//完了
|
140
|
+
|
7
|
-
SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
|
141
|
+
SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
|
142
|
+
|
143
|
+
SharedPreferences.Editor editor = preferences.edit();
|
144
|
+
|
145
|
+
editor.putInt("count",1);
|
146
|
+
|
147
|
+
editor.commit();
|
8
148
|
|
9
149
|
```
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,9 @@
|
|
1
1
|
MainActivityのメソッドをほかのアクティビティーから呼び出すためにインスタンスを生成して実行したのですが、「SharedPreferences」を使っている部分で「java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference」というエラーが出てしまいます。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
```java
|
6
|
+
|
7
|
+
SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
|
8
|
+
|
9
|
+
```
|