-1

我用过这个教程。

我正在开发基于蓝牙的灯控制器应用程序,在我的应用程序中,我想使用手势滑动从一个灯组移动到另一组,即坏房间灯到客厅灯,当我在屏幕上滑动时,我的应用程序抛出"stooped unexpectedly"异常。请帮助我这是我的代码:

package com.taher.bluetoothremotecontrol;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.app.Activity;
import android.view.Menu;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class ClientSocketActivity  extends Activity implements OnGestureListener
{
    //private static final String TAG = ClientSocketActivity.class.getSimpleName();
    private static final int REQUEST_DISCOVERY = 0x1;;
    private Handler _handler = new Handler();
    private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
    private BluetoothSocket socket = null;
    private OutputStream outputStream;
    private InputStream inputStream;
    private StringBuffer sbu;
    private GestureDetector gDetector;
    ViewFlipper flipper;
    SeekBar seekbar;
    TextView value;
    byte sValue;
    int p;
    Button D1,D2,D3,D4,All,BACK,EXIT,L1,L2,L3,L4;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    /*private Animation inFromRightAnimation() {

        Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(500);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
        }
       private Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
          Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoLeft.setDuration(500);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
        }
       private Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromLeft.setDuration(500);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
        }
       private Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
          Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoRight.setDuration(500);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
        }*/

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        setContentView(R.layout.main_2);
        if (!_bluetooth.isEnabled()) {
            Toast.makeText(getApplicationContext(),"Bluetooth is not enable", Toast.LENGTH_SHORT).show();
            finish();
            return;
            }
        Log.d("EF-BTBee", ">>setOnKeyListener");
        Intent intent = new Intent(this, DiscoveryActivity.class);

        /* Prompted to select a server to connect */
        Toast.makeText(this, "select device to connect", Toast.LENGTH_SHORT).show();

        /* Select device for list */
        startActivityForResult(intent, REQUEST_DISCOVERY);
        final Button L1   = (Button) findViewById(R.id.button1);
        final Button L2   = (Button) findViewById(R.id.button2);
        final Button L3   = (Button) findViewById(R.id.button3);
        final Button L4   = (Button) findViewById(R.id.button4);

        L4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String tmpStr="32\r";
                byte bytes[] = tmpStr.getBytes();
                try {outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        L3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String tmpStr="34\r";
                byte bytes[] = tmpStr.getBytes();
                try {outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        L1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String tmpStr="00\r";
                byte bytes[] = tmpStr.getBytes();
                try {outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        L2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String tmpStr="36\r";
                byte bytes[] = tmpStr.getBytes();
                try {outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });        
    }

    /* after select, connect to device */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != REQUEST_DISCOVERY) {
            finish();
            return;
        }
        if (resultCode != RESULT_OK) {
            finish();
            return;
        }
        final BluetoothDevice device = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        new Thread() {
            public void run() {
                connect(device);
            };
        }.start();
    }

    protected void onDestroy() {
        super.onDestroy();
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("EF-BTBee", ">>", e);
        }
    }

    protected void connect(BluetoothDevice device) {
        //BluetoothSocket socket = null;
        try {
            //Create a Socket connection: need the server's UUID number of registered
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));

            socket.connect();
            Log.d("EF-BTBee", ">>Client connectted");

            inputStream = socket.getInputStream();                                                        
            outputStream = socket.getOutputStream();
            int read = -1;
            final byte[] bytes = new byte[2048];
            for (; (read = inputStream.read(bytes)) > -1;) {
                final int count = read;
                _handler.post(new Runnable() {
                    public void run() {
                        StringBuilder b = new StringBuilder();
                        for (int i = 0; i < count; ++i) {
                        String s = Integer.toString(bytes[i]);
                            b.append(s);
                            b.append(",");
                        }
                        String s = b.toString();
                        String[] chars = s.split(",");
                        sbu = new StringBuffer();  
                         for (int i = 0; i < chars.length; i++) {  
                                sbu.append((char) Integer.parseInt(chars[i]));  
                            }
                        Log.d("EF-BTBee", ">>inputStream");
                    }
                });
            }

        } catch (IOException e) {
            Log.e("EF-BTBee", ">>", e);
            finish();
            return ;
        } finally {
            if (socket != null) {
                try {
                    Log.d("EF-BTBee", ">>Client Socket Close");
                    socket.close();    
                    finish();
                    return ;
                } catch (IOException e) {
                  Log.e("EF-BTBee", ">>", e);
                }
            }
        }
    }
      public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }
       public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
           try {
               if (start.getX() > finish.getX() && Math.abs(start.getX() - finish.getX()) > SWIPE_MIN_DISTANCE && Math.abs(xVelocity) > SWIPE_THRESHOLD_VELOCITY) {

                   //flipper.setInAnimation(inFromRightAnimation());
                   //flipper.setOutAnimation(outToLeftAnimation());
                   flipper.showNext();      
               } else if (start.getX() < finish.getX() && finish.getX() - start.getX() > SWIPE_MIN_DISTANCE && Math.abs(xVelocity) > SWIPE_THRESHOLD_VELOCITY){

                   //flipper.setInAnimation(inFromLeftAnimation());
                   //flipper.setOutAnimation(outToRightAnimation());
                   flipper.showPrevious();
               }
           } catch (Exception e) {
            // nothing
        }
               return true;
    }

       public void onLongPress(MotionEvent arg0) {}
       public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {return false;}
       public void onShowPress(MotionEvent arg0) {}
       public boolean onSingleTapUp(MotionEvent arg0) {return false;}
       @Override
       public boolean onTouchEvent(MotionEvent me) {return gDetector.onTouchEvent(me);}
}
4

1 回答 1

0
 private GestureDetector gDetector;

你永远不会初始化gDetector.

里面onCreate()

gDetector = new GestureDetector(this);

于 2013-04-29T13:02:15.560 回答