質問編集履歴
3
サブクラスの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -96,7 +96,28 @@
|
|
96
96
|
nameText.setText(deviceName);
|
97
97
|
}
|
98
98
|
```
|
99
|
+
```java
|
100
|
+
private MainActivity m = new MainActivity();
|
99
101
|
|
102
|
+
//ペアデバイスを取得してサーバーに接続する
|
103
|
+
public void scanAndConnect(Context context,BluetoothAdapter bluetoothAdapter){
|
104
|
+
pairedDevices = bluetoothAdapter.getBondedDevices();
|
105
|
+
if (pairedDevices.size() > 0) {
|
106
|
+
for (BluetoothDevice device : pairedDevices) {
|
107
|
+
Log.d("pairedDevice","Name:"+device.getName()+"Address:"+device.getAddress());
|
108
|
+
Toast.makeText(context,"接続を開始します",Toast.LENGTH_SHORT).show();
|
109
|
+
device = bluetoothAdapter.getRemoteDevice(device.getAddress());
|
110
|
+
bluetoothGatt = device.connectGatt(context, false, bluetoothGattCallback);
|
111
|
+
m.setTextName("" + device.getName());
|
112
|
+
}
|
113
|
+
}
|
114
|
+
if (bluetoothGatt == null) {
|
115
|
+
Toast.makeText(context, "ペアリングされたデバイスが存在しません", Toast.LENGTH_SHORT).show();
|
116
|
+
}
|
117
|
+
Toast.makeText(context,"Bluetooth通信に異常が発生しています",Toast.LENGTH_LONG).show();
|
118
|
+
}
|
119
|
+
```
|
120
|
+
|
100
121
|
### 試したこと
|
101
122
|
|
102
123
|
onCreateにfindViewByIdを書いてみた
|
2
エラーメッセージを変更しそびれていたので変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,18 +10,11 @@
|
|
10
10
|
|
11
11
|
```
|
12
12
|
E/AndroidRuntime: FATAL EXCEPTION: main
|
13
|
-
Process: com.example.esp32ble, PID:
|
13
|
+
Process: com.example.esp32ble, PID: 29278
|
14
|
-
java.lang.NullPointerException: Attempt to invoke virtual method '
|
14
|
+
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
|
15
|
-
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
|
16
|
-
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
|
17
|
-
at android.content.Context.obtainStyledAttributes(Context.java:738)
|
18
|
-
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
|
19
|
-
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
|
20
|
-
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
|
21
|
-
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
|
22
|
-
at com.example.esp32ble.MainActivity.setTextName(MainActivity.java:
|
15
|
+
at com.example.esp32ble.MainActivity.setTextName(MainActivity.java:174)
|
23
16
|
at com.example.esp32ble.SubsidyBle.scanAndConnect(SubsidyBle.java:121)
|
24
|
-
at com.example.esp32ble.MainActivity.chartChange(MainActivity.java:
|
17
|
+
at com.example.esp32ble.MainActivity.chartChange(MainActivity.java:160)
|
25
18
|
at com.example.esp32ble.-$$Lambda$TW4Z-_TbHlSaQ3_9pMVQ_fCEm0w.onClick(Unknown Source:2)
|
26
19
|
at android.view.View.performClick(View.java:7217)
|
27
20
|
at android.view.View.performClickInternal(View.java:7191)
|
@@ -34,6 +27,7 @@
|
|
34
27
|
at java.lang.reflect.Method.invoke(Native Method)
|
35
28
|
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
|
36
29
|
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
|
30
|
+
|
37
31
|
```
|
38
32
|
|
39
33
|
### 該当のソースコード
|
1
onCreateにfindViewByIdの移動をしTextViewのnameTextをグローバル変数にした
title
CHANGED
File without changes
|
body
CHANGED
@@ -45,6 +45,7 @@
|
|
45
45
|
private LineChart chart;
|
46
46
|
private boolean connectState = false;
|
47
47
|
private Button changeButton;
|
48
|
+
private TextView nameText;
|
48
49
|
private final String NOW_CONNECT = "接続を終了";
|
49
50
|
private final String NOW_DISCONNECT = "接続を開始";
|
50
51
|
|
@@ -66,7 +67,8 @@
|
|
66
67
|
|
67
68
|
changeButton = findViewById(R.id.chart_change);
|
68
69
|
changeButton.setOnClickListener(this::chartChange);
|
69
|
-
|
70
|
+
nameText = findViewById(R.id.device_name_text);
|
71
|
+
|
70
72
|
lineChart();
|
71
73
|
intent = new Intent();
|
72
74
|
subBle = new SubsidyBle();
|
@@ -97,7 +99,6 @@
|
|
97
99
|
//subClassで検出したデバイスの名前を受け取って表示
|
98
100
|
public void setTextName(String deviceName){
|
99
101
|
Log.d("kiteru?",deviceName);
|
100
|
-
TextView nameText = findViewById(R.id.device_name_text);
|
101
102
|
nameText.setText(deviceName);
|
102
103
|
}
|
103
104
|
```
|