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

时间:2016-03-30 06:54:53

标签: android bluetooth crash handler

全部,

我非常感谢一些帮助。我们有一个以2 Hz(每0.5秒)发送数据的系统。我的应用程序应该在按下" start"按钮通过一个处理程序,按下"停止"按钮,用户可以选择保存收集的数据。如果选择了save,则会启动一个新活动,但是,我的应用程序崩溃了。错误消息:

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) { }
        }
    }

}

对此有何想法?我完全没有关于导致此崩溃的原因的想法......可能是蓝牙连接的东西?

谢谢, 约翰

2 个答案:

答案 0 :(得分:2)

fromBluetooth为null,您无法在null String上调用indexOf(...)。

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

享受

答案 1 :(得分:1)

替换您的代码

 if(fromBluetooth!=null){
                tempString = fromBluetooth.substring(fromBluetooth.indexOf('\t')+1);
            }

fromBluetooth.substring(fromBluetooth.indexOf('\t')+1);

因为你没有检查字符串是否为null,而是使用indexOf();

直接查找值