teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

誤字

2017/02/24 03:29

投稿

negisuki
negisuki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -61,7 +61,7 @@
61
61
  Suicaをかざした時点でアプリ終了
62
62
  エラー内容
63
63
  E/AndroidRuntime: FATAL EXCEPTION: Thread-11563
64
- Process: com.example.user.playertest, PID: 7454
64
+ Process: com.example.user.test, PID: 7454
65
65
  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference
66
66
  at android.nfc.tech.Ndef.get(Ndef.java:146)
67
67
  at com.example.user.test.MainActivity$SuicaCheck.run(MainActivity.java:144)

3

誤字

2017/02/24 03:29

投稿

negisuki
negisuki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -61,7 +61,7 @@
61
61
  Suicaをかざした時点でアプリ終了
62
62
  エラー内容
63
63
  E/AndroidRuntime: FATAL EXCEPTION: Thread-11563
64
- Process: com.example.nishi.playertest, PID: 7454
64
+ Process: com.example.user.playertest, PID: 7454
65
65
  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference
66
66
  at android.nfc.tech.Ndef.get(Ndef.java:146)
67
67
  at com.example.user.test.MainActivity$SuicaCheck.run(MainActivity.java:144)

2

追記

2017/02/24 03:28

投稿

negisuki
negisuki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -28,4 +28,47 @@
28
28
  このコードだと端末からSuicaを離すまで画面遷移(transaction)が実行されません。
29
29
  画面遷移をしつつ、Suicaと端末の状態をモニターするにはどのような機能を使い、どんなコードを書くべきでしょうか?
30
30
 
31
- よろしくお願いします。
31
+ よろしくお願いします。
32
+
33
+
34
+ __**追記:別threadを作成**__
35
+ ```Java
36
+ public void onNewIntent(Intent intent) {
37
+ transaction(intent); //画面遷移メソッドを呼び出し
38
+
39
+ SuicaCheck check = new SuicaCheck();
40
+ check.start();
41
+ }
42
+
43
+ class SuicaCheck extends Thread {
44
+ public void run(){
45
+ Intent intent = new Intent();
46
+ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
47
+ Ndef ndef = Ndef.get(tag);
48
+ try {
49
+ ndef.connect();
50
+ while(ndef.isConnected()){
51
+ Toast.makeText(this, "タッチしています", Toast.LENGTH_LONG).show();
52
+ }
53
+ Toast.makeText(this, "タグが離れました", Toast.LENGTH_LONG).show();
54
+ } catch (IOException e) {
55
+ Toast.makeText(this, "エラー", Toast.LENGTH_LONG).show();
56
+ }
57
+ }
58
+ ```
59
+
60
+ __実行結果__
61
+ Suicaをかざした時点でアプリ終了
62
+ エラー内容
63
+ E/AndroidRuntime: FATAL EXCEPTION: Thread-11563
64
+ Process: com.example.nishi.playertest, PID: 7454
65
+ java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference
66
+ at android.nfc.tech.Ndef.get(Ndef.java:146)
67
+ at com.example.user.test.MainActivity$SuicaCheck.run(MainActivity.java:144)
68
+
69
+ MainACtivityの144行目は
70
+ ```Java
71
+ Ndef ndef = Ndef.get(tag);
72
+ ```
73
+ です。tagが他が正しく値を持ってこれていないのでしょうか、
74
+ onNewIntentにあった時との違いがいまいち分かりません・・・

1

誤字

2017/02/24 01:55

投稿

negisuki
negisuki

スコア12

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,7 @@
3
3
  SuicaをAndroid端末にかざすと画面遷移するアプリを作成しています。
4
4
  これだけなら簡単なのですが、
5
5
  かざしていたSuicaを端末から離した時に元の画面に戻りたいのです。
6
- なお、遷移先の画面BはFragmentで、MainActivityの上に乗っけています。
6
+ なお、遷移先の画面はFragmentで、MainActivityの上に乗っけています。
7
7
  Suicaを離した時はこの画面Bを削除することで元のMainActivityを再表示するイメージです。
8
8
 
9
9
  現在のコードは