java.lang.NumberFormatException:无效的int:“”

时间:2016-02-16 09:17:28

标签: android bluetooth arduino numberformatexception

Android App:

 private static final String TAG = "HC-05";   
  Button btnReceive;
  TextView txtReading;
  Handler h;
  ImageView imv1, imv2, imv3, imv4;
  int s1=0, s2=0;

  final int RECIEVE_MESSAGE = 1;        // Status  for Handler
  private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private StringBuilder sb = new StringBuilder();

  private ConnectedThread mConnectedThread;

  // SPP UUID service
  private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  // MAC-address of Bluetooth module (you must edit this line)
  private static String address = "98:D3:31:80:57:0D";

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



    imv1 =(ImageView) findViewById(R.id.imageView1);
    imv2 =(ImageView) findViewById(R.id.imageView2);
    imv3 =(ImageView) findViewById(R.id.imageView3);
    imv4 =(ImageView) findViewById(R.id.imageView4);
        btnReceive = (Button) findViewById(R.id.btnReceive);                // button LED OFF
        txtReading = (TextView) findViewById(R.id.txtReading);

         h = new Handler() {
                public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                    case RECIEVE_MESSAGE:                                                   // if receive massage
                        byte[] readBuf = (byte[]) msg.obj;
                        String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                        sb.append(strIncom);                                                // append string
                        int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                        if (endOfLineIndex > 0) {                                            // if end-of-line,
                            String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                            sb.delete(0, sb.length());                                      // and clear
                            txtReading.setText("Data from Arduino: " + sbprint);
                            //System.out.println(sbprint);
                            **if(sbprint!=null || !"0".equals(sbprint)){
                                 s2 = Integer.parseInt(sbprint);
                                 initOpacity();
                            }**

                            // update TextView
                            btnReceive.setEnabled(true);

                           // if(!"".equals(sbprint)){

                         //   }
                          //  else
                         //   {
                          //    s2=0;
                          //  }

                             System.out.println("================== VAlUE BT = " +s2+ "===================");

                        }


                        //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                        break;
                    }
                };
            };


            btAdapter = BluetoothAdapter.getDefaultAdapter();       // get Bluetooth adapter
            checkBTState();

        /*    btnReceive.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  btnReceive.setEnabled(false);
                  mConnectedThread.write("1");    // Send "1" via Bluetooth
                //Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
              }
            });*/




    }


     private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
          if(Build.VERSION.SDK_INT >= 10){
              try {
                  final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                  return (BluetoothSocket) m.invoke(device, MY_UUID);
              } catch (Exception e) {
                  Log.e(TAG, "Could not create Insecure RFComm Connection",e);
              }
          }
          return  device.createRfcommSocketToServiceRecord(MY_UUID);
      }

      @Override
      public void onResume() {
        super.onResume();

        Log.d(TAG, "...onResume - try connect...");

        // Set up a pointer to the remote node using it's address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);

        // Two things are needed to make a connection:
        //   A MAC address, which we got above.
        //   A Service ID or UUID.  In this case we are using the
        //     UUID for SPP.

        try {
            btSocket = createBluetoothSocket(device);
        } catch (IOException e) {
            errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        btAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(TAG, "...Connecting...");
        try {
          btSocket.connect();
          Log.d(TAG, "....Connection ok...");
        } catch (IOException e) {
          try {
            btSocket.close();
          } catch (IOException e2) {
            errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
          }
        }

        // Create a data stream so we can talk to server.
        Log.d(TAG, "...Create Socket...");

        mConnectedThread = new ConnectedThread(btSocket);
        mConnectedThread.start();
      }

      @Override
      public void onPause() {
        super.onPause();

        Log.d(TAG, "...In onPause()...");

        try     {
          btSocket.close();
        } catch (IOException e2) {
          errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
        }
      }

      private void checkBTState() {
        // Check for Bluetooth support and then check to make sure it is turned on
        // Emulator doesn't support Bluetooth and will return null
        if(btAdapter==null) {
          errorExit("Fatal Error", "Bluetooth not support");
        } else {
          if (btAdapter.isEnabled()) {
            Log.d(TAG, "...Bluetooth ON...");
          } else {
            //Prompt user to turn on Bluetooth
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
          }
        }
      }

      private void errorExit(String title, String message){
        Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
        finish();
      }

      private class ConnectedThread extends Thread {
            private final InputStream mmInStream;
            private final OutputStream mmOutStream;

            public ConnectedThread(BluetoothSocket socket) {
                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the input and output streams, using temp objects because
                // member streams are final
                try {
                    tmpIn = socket.getInputStream();
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) { }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
            }

            public void run() {
                byte[] buffer = new byte[256];  // buffer store for the stream
                int bytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs
                while (true) {
                    try {
                        // Read from the InputStream
                        bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                        h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                    } catch (IOException e) {
                        break;
                    }
                }
            }

            /* Call this from the main activity to send data to the remote device */
            public void write(String message) {
                Log.d(TAG, "...Data to send: " + message + "...");
                byte[] msgBuffer = message.getBytes();
                try {
                    mmOutStream.write(msgBuffer);
                } catch (IOException e) {
                    Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
                  }
            }
        }

      public void initOpacity()
        {


          if( s2 < 100)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha1);
            imv1.startAnimation(an);
        }
        else if(s2 > 100 && s2 < 200)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha2);
            imv1.startAnimation(an);
        }
        else if(s2 > 200 && s2 < 300)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha3);
            imv1.startAnimation(an);
        }
        else if(s2 > 300 && s2 < 400)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha4);
            imv1.startAnimation(an);
        }
        else if(s2 > 400 && s2 < 500)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha5);
            imv1.startAnimation(an);
        }
        else if(s2 > 500 && s2 < 600)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha6);
            imv1.startAnimation(an);
        }
        else if(s2 > 600 && s2 < 700)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha7);
            imv1.startAnimation(an);
        }
        else if(s2 > 700 && s2 < 800)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha8);
            imv1.startAnimation(an);
        }
        else if(s2 > 800 && s2 < 900)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha9);
            imv1.startAnimation(an);
        }
        else if(s2 > 900)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha10);
            imv1.startAnimation(an);            
        }


        //i2

        if( s2 < 100)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha1);
            imv2.startAnimation(an);
        }
        else if(s2 > 100 && s2 < 200)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha2);
            imv2.startAnimation(an);
        }
        else if(s2 > 200 && s2 < 300)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha3);
            imv2.startAnimation(an);
        }
        else if(s2 > 300 && s2 < 400)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha4);
            imv2.startAnimation(an);
        }
        else if(s2 > 400 && s2 < 500)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha5);
            imv2.startAnimation(an);
        }
        else if(s2 > 500 && s2 < 600)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha6);
            imv2.startAnimation(an);
        }
        else if(s2 > 600 && s2 < 700)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha7);
            imv2.startAnimation(an);
        }
        else if(s2 > 700 && s2 < 800)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha8);
            imv2.startAnimation(an);
        }
        else if(s2 > 800 && s2 < 900)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha9);
            imv2.startAnimation(an);
        }
        else if(s2 > 900)
        {
            Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha10);
            imv2.startAnimation(an);            
        }



        //Right Foot Blank
        Animation an1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha0);
        imv3.startAnimation(an1);
        Animation an2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha0);
        imv4.startAnimation(an2);    
        }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}

Arduino蓝牙:

#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Genotronex(10, 11); // RX, TX

int BluetoothData; // the data given from Computer
int sensorPin = A0;
int sensorValue = 0;
int ledPin = 13;  


void setup() {
Genotronex.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin);



Genotronex.println(sensorValue);

delay(500);

}

logcat的:

01-29 17:17:11.637: I/System.out(1776): ================== VAlUE BT = 0===================
01-29 17:17:11.797: I/System.out(1776): ================== VAlUE BT = 777===================
01-29 17:17:12.327: I/System.out(1776): ================== VAlUE BT = 975===================
01-29 17:17:12.807: I/System.out(1776): ================== VAlUE BT = 669===================
01-29 17:17:13.327: I/System.out(1776): ================== VAlUE BT = 971===================
01-29 17:17:13.817: I/System.out(1776): ================== VAlUE BT = 224===================
01-29 17:17:14.327: D/AndroidRuntime(1776): Shutting down VM
01-29 17:17:14.327: E/AndroidRuntime(1776): FATAL EXCEPTION: main
01-29 17:17:14.327: E/AndroidRuntime(1776): Process: com.example.blutooth, PID: 1776
01-29 17:17:14.327: E/AndroidRuntime(1776): java.lang.NumberFormatException: Invalid int: "
"
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.Integer.invalidInt(Integer.java:138)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.Integer.parse(Integer.java:410)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.Integer.parseInt(Integer.java:367)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.Integer.parseInt(Integer.java:334)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at com.example.blutooth.MainActivity$1.handleMessage(MainActivity.java:88)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at android.os.Looper.loop(Looper.java:145)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at android.app.ActivityThread.main(ActivityThread.java:5944)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invoke(Native Method)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invoke(Method.java:372)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
01-29 17:17:14.327: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

当传感器的值低于10时,我的应用程序崩溃了。 我试图过滤's2'以填充来自arduino的数据,只有当存在整数值时。但不知怎的,它仍然以同样的错误崩溃。 我在这里错过了什么吗?

3 个答案:

答案 0 :(得分:0)

发生java.lang.NumberFormatException,因此您检查字符串是否为空或null然后转换为int

         if(sbprint!=null || !"0".equals(sbprint)){
                             s2 = Integer.parseInt(sbprint);
                             initOpacity();
                        }

改为

                    if((sbprint!= null) && (!sbprint.isEmpty())){
                     isNumeric(String sbprint) 
                            {
                             s2 = Integer.parseInt(sbprint);
                             initOpacity();
                            }
                            else
                               {
                             //your string not in numeric format
                                }
                        }

并使用下面的代码检查您的字符串是否为数字格式。source

public static boolean isNumeric(String str)  
{  
try  
{  
double d = Double.parseDouble(str);  
}  
catch(NumberFormatException nfe)  
{  
return false;  
 }  
 return true;  
}

答案 1 :(得分:0)

如果我弄错了..检查字符串\n ..

if(sbprint!=null && !("0".equals(sbprint)) && !(sbprint.contains("\n"))){
               s2 = Integer.parseInt(sbprint);
               initOpacity();
             }

答案 2 :(得分:0)

好的,所以它现在不会崩溃。

事实证明问题出现在这一行:String sbprint = sb.substring(0, endOfLineIndex);

我在行尾添加了trim(),现在没有问题。

 String sbprint = sb.substring(0, endOfLineIndex).trim(); 

我还使用了来自@sasikumar的if语句:

if(sbprint!=null && (!sbprint.isEmpty())                                                                                                                                                                                                                                                                                                                                                                                                                                                       && !(sbprint.contains("\n"))){
     s2 = Integer.parseInt(sbprint);
     initOpacity();
}

感谢您的帮助!