gpt4 book ai didi

android - 为什么有时我的应用程序崩溃?

转载 作者:行者123 更新时间:2023-12-03 16:22:01 25 4
gpt4 key购买 nike

所有,

我将不胜感激一些帮助。我们有一个系统正在以2 Hz(每0.5秒)发送一次数据。我的应用程序应该在通过处理程序按下“开始”按钮时开始获取此数据,并且在按下“停止”按钮后,用户可以选择保存收集的数据。如果选择保存,则应该启动一个新 Activity ,但是,我的应用程序崩溃了。错误信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.indexOf(int)' on a null object reference
at com.example.cherryjp.buttonapp.StartNewSessionActivity$2.onClick(StartNewSessionActivity.java:130)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

和我的代码:
    package com.example.cherryjp.buttonapp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Set;
import java.util.Timer;
import java.util.UUID;

/**
*/
public class StartNewSessionActivity extends AppCompatActivity {

final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

long startTime;
Double endTime;
TextView watch;
Double totalSeconds;
Double seconds;
Double minutes;
Double hours;
String time;
boolean recording = false;
String fromBluetooth;


BluetoothAdapter mBluetoothAdapter;
ArrayAdapter mArrayAdapter;

final Context context = this;

final int REFRESH_RATE = 100;
Stopwatch timer = new Stopwatch();




Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;

switch (msg.what) {
case MSG_START_TIMER:
timer.start(); //start timer
mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
break;

case MSG_UPDATE_TIMER:
watch.setText("" + timer.toString());
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER, REFRESH_RATE); //text view is updated every second,
if(writeBuf == null){
break;
}
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
fromBluetooth += writeMessage;
fromBluetooth += "\t";
break;
//though the timer is still running
case MSG_STOP_TIMER:
mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.
timer.stop();//stop timer
//send reset signal for bluetooth


if(recording) {
launchSaveDialog();
}
recording = false;
watch.setText("" + timer.toString());

break;

default:
break;
}
}

};
private BluetoothDevice mDevice;

public void launchSaveDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage("Would you like to save that run?")
.setTitle("Save confirmation");

AlertDialog.Builder builder1 = builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

Entry entry = new Entry();
entry.setDate(MySQLiteHelper.getCurrentDateTime(context));
entry.setRiderId(getIntent().getExtras().getLong("RIDER_ID"));


//TODO: loop to pull values from bluetooth transmission
//String fromBluetooth = "5\t1\t3\t2\t5\t3\t3\t4";
String tempString = fromBluetooth;
tempString = fromBluetooth.substring(fromBluetooth.indexOf('\t')+1);
entry.setForceAndTimeTabSeparated(tempString);

MainActivity.riderdb.addEntry(entry);

Toast.makeText(getApplicationContext(),
"Run saved!", Toast.LENGTH_SHORT).show();



showGraph(entry);

}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});

AlertDialog dialog = builder.create();
dialog.show();
}

public void showGraph(Entry entry){
Intent immediateGraphIntent = new Intent(this, ImmediateGraph.class);

final int result = 1;

//Just getEntry on next intent instead of storing extras.
immediateGraphIntent.putExtra("ENTRY_ID", entry.getId());

//immediateGraphIntent.putExtra("PASSTIME", timeArray);
//immediateGraphIntent.putExtra("PASSFORCE", forceArray);

startActivity(immediateGraphIntent);

}


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.session_start_page);

Button startButton = (Button) findViewById(R.id.startsessionbutton);
Button endButton = (Button) findViewById(R.id.endsessionbutton);

watch = (TextView)findViewById(R.id.watch);


mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDevice = device;
}
}
final ConnectThread mConnectThread = new ConnectThread(mDevice);
mConnectThread.start();

startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//START recording stuff.
recording = true;
while(mConnectThread.mConnectedThread == null){

}
mConnectThread.mConnectedThread.write("begin".getBytes());
mHandler.sendEmptyMessage(MSG_START_TIMER);
}
});

endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//END recording stuff
mHandler.sendEmptyMessage(MSG_STOP_TIMER);
}
});
}


public class Stopwatch {
private long startTime = 0;
private boolean running = false;
private long currentTime = 0;

public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
}

public void stop() {
this.running = false;
}

public void pause() {
this.running = false;
currentTime = System.currentTimeMillis() - startTime;
}
public void resume() {
this.running = true;
this.startTime = System.currentTimeMillis() - currentTime;
}

//elaspsed time in milliseconds
public long getElapsedTimeMili() {
long elapsed = 0;
if (running) {
elapsed =((System.currentTimeMillis() - startTime)/100) % 10 ;
}
return elapsed;
}

//elaspsed time in seconds
public long getElapsedTimeSecs() {
long elapsed = 0;
if (running) {
elapsed = ((System.currentTimeMillis() - startTime) / 1000) % 60;
}
return elapsed;
}

//elaspsed time in minutes
public long getElapsedTimeMin() {
long elapsed = 0;
if (running) {
elapsed = (((System.currentTimeMillis() - startTime) / 1000) / 60 ) % 60;
}
return elapsed;
}

//elaspsed time in hours
public long getElapsedTimeHour() {
long elapsed = 0;
if (running) {
elapsed = ((((System.currentTimeMillis() - startTime) / 1000) / 60 ) / 60);
}
return elapsed;
}

public String toString() {
return String.format("%1$02d:%2$02d:%3$02d.%4$01d", getElapsedTimeHour(), getElapsedTimeMin(), getElapsedTimeSecs(), getElapsedTimeMili());
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private ConnectedThread mConnectedThread;

public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);
for(int i = begin; i < bytes; i++) {
if(buffer[i] == "\n".getBytes()[0]) {
mHandler.obtainMessage(MSG_UPDATE_TIMER, begin, i, buffer).sendToTarget();
begin = i + 1;
if(i == bytes - 1) {
bytes = 0;
begin = 0;
}
}
}
} catch (IOException e) {
break;
}
}
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

}

有什么想法吗?我完全不知道是什么原因导致崩溃的……也许是通过蓝牙连接的?

谢谢,
约翰

最佳答案

fromBluetooth为null,您不能在null String上调用indexOf(...)。

因此,您必须确保fromBluetooth不为null(好)或处理fromBluetooth为null(更好)的情况(例如if(fromBluetooth!= null)执行逻辑)。

请享用

关于android - 为什么有时我的应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com