我正在尝试从arduino到蓝牙电话进行温度显示,但我不明白为什么它不起作用
我试图对代码的某些部分进行注释,有时它不能与某件事一起使用,有时却可以工作,这很奇怪
activity1 java(主要)
package it.aletinti.apkduino;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class activity1 extends AppCompatActivity {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
ListView btDeviceList;
String btDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
Intent btEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(btEnable, 1);
btDeviceList = findViewById(R.id.btDeviceList);
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
List<String> ArrayList = new ArrayList();
for(BluetoothDevice btAdapter : pairedDevices) ArrayList.add(btAdapter.getName());
final ArrayAdapter btDeviceArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ArrayList);
btDeviceList.setAdapter(btDeviceArrayAdapter);
btDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
btDevice = (btDeviceArrayAdapter.getItem(position)).toString();
activity2();
}
});
}
public void activity2(){
Intent NextActivity = new Intent(this, activity2.class);
NextActivity.putExtra("btDevice", btDevice);
startActivity(NextActivity);
}
}
activity1 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark"
tools:context=".activity1">
<ListView
android:id="@+id/btDeviceList"
android:layout_width="339dp"
android:layout_height="629dp"
android:layout_marginTop="60dp"
android:layout_marginBottom="16dp"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/selectTextView"
android:layout_width="175dp"
android:layout_height="20dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Select a device to connect"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/btDeviceList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity2 java(崩溃或未运行)
package it.aletinti.apkduino;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class activity2 extends AppCompatActivity {
private static final UUID btUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
;
TextView stateText, btText, adText, maxText, minText;
SeekBar maxBar, minBar;
int minTemp, maxTemp;
BluetoothSocket btSocket = null;
Button updateButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
maxBar = this.findViewById(R.id.maxBar);
minBar = this.findViewById(R.id.minBar);
updateButton = this.findViewById(R.id.updateButton);
maxText = this.findViewById(R.id.maxText);
minText = this.findViewById(R.id.minText);
stateText = this.findViewById(R.id.stateText);
maxBar.setProgress(3);
minBar.setProgress(3);
final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Bundle getbtDevice = getIntent().getExtras();
String btDeviceName = getbtDevice.getString("btDevice");
new Thread(new Runnable() {
@Override
public void run() {
try {
BluetoothDevice btDevice = btAdapter.getRemoteDevice(/*btDeviceName*/"AB:90:78:57:C2:5A" /*USING MAC FOR MORE SURE PAIR ONLY IN DEBUG*/);
btSocket = btDevice.createRfcommSocketToServiceRecord(btUUID);
btSocket.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (!isDestroyed()/*or true*/) {
byte[] buffer = new byte[1024];
int bytes = btSocket.getInputStream().read(buffer, 0, buffer.length);
String readMessage = new String(buffer, 0, bytes);
System.out.println(readMessage);
final String t = readMessage.split("/")[0];
final String h = readMessage.split("/")[1];
runOnUiThread(new Runnable() {
@Override
public void run() {
stateText.setText("Temperature: " + t + "°" + "\nHumidity: " + h + "%");
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
maxTemp = 24 + maxBar.getProgress();
minTemp = 20 - minBar.getProgress();
runOnUiThread(new Runnable() {
@Override
public void run() {
minText.setText("Min: " + minTemp);
maxText.setText("Max: " + maxTemp);
}
});
}
}
}).start();
}
}
activity2 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark"
tools:context=".activity2">
<TextView
android:id="@+id/stateText"
android:layout_width="298dp"
android:layout_height="166dp"
android:layout_marginStart="32dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="32dp"
android:gravity="center"
android:text="Temperature:\nHumidity:"
android:textColor="@color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/maxText"
android:layout_width="136dp"
android:layout_height="19dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="MAX"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/maxBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/updateButton"
android:layout_width="253dp"
android:layout_height="37dp"
android:layout_marginBottom="50dp"
android:background="@color/arduino"
android:text="Update"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<SeekBar
android:id="@+id/minBar"
android:layout_width="250dp"
android:layout_height="25dp"
android:layout_marginBottom="50dp"
android:layoutDirection="rtl"
android:max="8"
android:min="0"
app:layout_constraintBottom_toTopOf="@+id/updateButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<SeekBar
android:id="@+id/maxBar"
android:layout_width="250dp"
android:layout_height="25dp"
android:layout_marginBottom="40dp"
android:layoutDirection="ltr"
android:max="8"
android:min="0"
app:layout_constraintBottom_toTopOf="@+id/minText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/minText"
android:layout_width="136dp"
android:layout_height="19dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="MIN"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/minBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
错误
Process: it.aletinti.apkduino, PID: 26745
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getWidth()' on a null object reference
at android.widget.TextView.onMeasure(TextView.java:8637)
at android.support.v7.widget.AppCompatTextView.onMeasure(AppCompatTextView.java:501)
at android.view.View.measure(View.java:23181)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:23181)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23181)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
at android.view.View.measure(View.java:23181)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23181)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23181)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
at android.view.View.measure(View.java:23181)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2727)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1580)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1864)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1468)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7208)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1004)
at android.view.Choreographer.doCallbacks(Choreographer.java:816)
at android.view.Choreographer.doFrame(Choreographer.java:751)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:990)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6694)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
有时会运行但不加载温度,其他时候如果不输入活动1就会崩溃,而其他时候我输入活动2则会崩溃。我是android的新手,但我不明白为什么此代码会崩溃!!!
更新:现在不会崩溃,但不会读写信号,而是链接蓝牙