2016-03-30 10 views
0

すべて、アプリケーションがクラッシュすることがあるのはなぜですか?

私は非常に助けていただければ幸いです。我々は、2Hz(0.5秒ごと)でデータを送信するシステムを持っています。私のアプリケーションは、ハンドラを介して「開始」ボタンを押すとこのデータの取得を開始し、「停止」ボタンを押すと、収集されたデータを保存することを選択できます。 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

fromBluetoothがnullである、とあなたがヌル文字列でのindexOf(...)を呼び出すことはできません。

したがって、fromBluetoothがnull(正常)でないこと、またはfromBluetoothがnull(より良い)(たとえば、fromBluetooth!= null)ロジックを実行するケースを処理する必要があります。

は、文字列をチェックしていないがヌルかどうかと、直接のindexOf()を使用してfindvalueあるので、あなたのコード

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

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

を交換し

1

をお楽しみください。

関連する問題