调试或运行时程序强制关闭

时间:2012-03-12 08:33:19

标签: android

以前我的程序在不同尺寸的屏幕和设备上运行良好。在我为x-large屏幕添加了一些按钮并为所有屏幕尺寸编辑了不同的布局后,当我在x-large之外的所有布局上运行它时,设备会提示我强行关闭程序。有什么问题??

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //**********************1 inch is about 160dp*********************
     setContentView(R.layout.main);

     Display display = getWindowManager().getDefaultDisplay();

     int width = display.getWidth();  
     //int height = display.getHeight();  

     //for different sizes of screens, set range of position of scrollbars
     if(width > 1200)
     {
         maxY = 325;
         minY = 275;
         maxX = 700;
         minX = 600;
         limitY = 210;
         limitY2 = 360;
         axisSelector = 1; //use x
     }

     else
     {
         //to be edited
         axisSelector = 0;
         maxY = 250;
         minY = 200;
         maxX = 500;
         minX = 400;
         limitY = 120;
         limitY2 = 270;

     }

    mainLayout = (LinearLayout) findViewById(R.id.controlLayout);
    mainLayout.setOnTouchListener(this);

    localBtDevice = (TextView) findViewById(R.id.txtLocalBtDevice);
    remoteBtDevices = (ListView) findViewById(R.id.lstRemoteBtDevices);
    btStatus = (TextView) findViewById(R.id.txtBtStatus);
    txtCalibrate = (TextView) findViewById(R.id.txtCalibrate);
    txtCalibrate.setText(String.valueOf(calValue));
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        btStatus.setText(" > Bluetooth not available!");
        return;
    }
    localBtDevice.setText("  " + btAdapter.getName());
    Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
    Iterator<BluetoothDevice> device = devices.iterator();
    final String[] deviceList = new String[devices.size()];
    final String[] addressList = new String[devices.size()];
    for (int i = 0; i < devices.size(); i++) {
        BluetoothDevice dev = device.next();
        deviceList[i] = dev.getName();
        addressList[i] = dev.getAddress();
    }
    remoteBtDevices.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, deviceList));
    deviceSelected = "";
    remoteBtDevices.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view, int pos, long id) {
            btStatus.setText(" > Connect to \"" + deviceList[pos] + "\"?");
            deviceSelected = deviceList[pos];
            addressSelected = addressList[pos];
            return false;
        }
    });

    btnBtAccess = (Button) findViewById(R.id.btnBtAccess);
    if (BtConnected == false) {
        btStatus.setText(" > Disconnected.");
        btnBtAccess.setText("Connect");
    }
    else {
        btStatus.append(" > You are connected to \"" + socketEP.getRemoteDevice().getName()+ "\".");
        btnBtAccess.setText("Close");
    }
    btnBtAccess.setOnClickListener(ConnectRobotListener);

    btnCalLeft = (Button) findViewById(R.id.btnCalLeft);
    btnCalLeft.setOnClickListener(CalLeftListener);
    btnCalRight = (Button) findViewById(R.id.btnCalRight);
    btnCalRight.setOnClickListener(CalRightListener);

    progressBarX = (ProgressBar) findViewById(R.id.ProgressBarHorz);
    progressBarX.setMax(200);
    progressBarX.setProgress(100);
    steeringText = (TextView)findViewById(R.id.txtSteering);

    progressBarY = (VerticalProgressBar) findViewById(R.id.ProgressBarVert);
    progressBarY.setMax(200);
    progressBarY.setProgress(100);
    throtleText = (TextView) findViewById(R.id.txtThrotle);

  //***********************************************************************************************  
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (btnBtAccess.getText() == "Close") {     
            button1count++;
            if(button1count % 2 == 0){
            button1.setText("Tilt Steer Enabled");
            selector++;
        }
            else{
            button1.setText("Tilt Steer Disabled");
            selector++;
            }
        }
        }
    });
  //************************************************************************************************** 
    button3 = (Button) findViewById(R.id.button3);
    button3.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnBtAccess.getText() == "Close") {     
            steerCountLeft++;
            if(steerCountLeft % 2 == 1)
            steer--;
            if(steer < 0)
               steer = 0;
            if(steer < 30)
            {
                try {
                    int calDirection = (int)((30 - steer) - calValue);                          
                    if (calDirection < 0)
                        calDirection = 0;
                    RoboController.outputStream.write('L');
                    RoboController.outputStream.write(calDirection);
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    steeringText.setText("L "+String.valueOf((int)(30 - steer)+" deg"));    
                    int steer1 = ( ((steer * 334) / 100));
                    progressBarX.setProgress(steer1);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }

            }
            else
            {
                try {
                    int calDirection = (int)((steer - 30) - calValue);                          
                    if (calDirection < 0)
                        calDirection = 0;
                    RoboController.outputStream.write('R');
                    RoboController.outputStream.write(calDirection);
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    steeringText.setText("R "+String.valueOf((int)(steer - 30)+" deg"));    
                    int steer1 = ( ((steer * 334) / 100));
                    progressBarX.setProgress(steer1);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }

            }
            }
            return true;
        }
    });
  //**************************************************************************************************  
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnBtAccess.getText() == "Close") {     
            steerCountRight++;
            if(steerCountRight % 2 == 1)
            steer++;
            if(steer > 60)
               steer = 60;
            if(steer > 30)
            {
                try {
                    int calDirection = (int)((steer - 30) - calValue);                          
                    if (calDirection < 0)
                        calDirection = 0;
                    RoboController.outputStream.write('R');
                    RoboController.outputStream.write(calDirection);
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    steeringText.setText("R "+String.valueOf((int)(steer - 30)+" deg"));    
                    steer1 = ( ((steer * 334) / 100));
                    progressBarX.setProgress(steer1);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }
            }
            else
            {
                try {
                    int calDirection = (int)((30 - steer) - calValue);                          
                    if (calDirection < 0)
                        calDirection = 0;
                    RoboController.outputStream.write('L');
                    RoboController.outputStream.write(calDirection);
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    steeringText.setText("L "+String.valueOf((int)(30 - steer)+" deg"));    
                    steer1 = ( ((steer * 334) / 100));
                    progressBarX.setProgress(steer1);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }
            }
            }
            return true;
        }
    });
    //***************************************************************************
    button4 = (Button) findViewById(R.id.button4);
    button4.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnBtAccess.getText() == "Close") {     
                move++;
            if(move > 100)
               move = 100;

            if(move > 0)
            {

                try {
                    RoboController.outputStream.write('F');
                    RoboController.outputStream.write((int)(move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("F "+String.valueOf((int)(move)+" cm/s"));
                    progressBarY.setProgress(100 + move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }

            }
            else
            {
                try {
                    RoboController.outputStream.write('B');
                    RoboController.outputStream.write((int)(-move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("B "+String.valueOf((int)(-move)+" cm/s"));
                    progressBarY.setProgress(100 + move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }
            }
            }


            return true;
        }
    });


    //**************************************************************************************
    button5 = (Button) findViewById(R.id.button5);
    button5.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnBtAccess.getText() == "Close") {     

            move--;
            if(move < -100)
               move = -100;
            if(move < 0)
            {
                try {
                    RoboController.outputStream.write('B');
                    RoboController.outputStream.write((int)(-move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("B "+String.valueOf((int)(-move)+" cm/s"));
                    progressBarY.setProgress(100+ move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }
            }
            else
            {
                try {
                    RoboController.outputStream.write('F');
                    RoboController.outputStream.write((int)(move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("F "+String.valueOf((int)(move)+" cm/s"));
                    progressBarY.setProgress(100 + move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }
            }
            }
            return true;
        }
    });




    //************************************************************************************
    button6 = (Button) findViewById(R.id.button6);
    button6.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (btnBtAccess.getText() == "Close") {     
                move = 100;
                try {
                    RoboController.outputStream.write('F');
                    RoboController.outputStream.write((int)(move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("F "+String.valueOf((int)(move)+" cm/s"));
                    progressBarY.setProgress(100 + move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }

            }

        }
    });


    //**********************************************************************************
    button7 = (Button) findViewById(R.id.button7);
    button7.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (btnBtAccess.getText() == "Close") {     
                move = 0;
                try {
                    RoboController.outputStream.write('F');
                    RoboController.outputStream.write((int)(move));
                    RoboController.outputStream.write('\r');
                    RoboController.outputStream.write('\n');
                    throtleText.setText("F "+String.valueOf((int)(move)+" cm/s"));
                    progressBarY.setProgress(100 + move);
                } catch (IOException e) {
                    btStatus.setText(" > Error sending data!");
                }

            }

        }
    });
    //**********************************************************************************
    button8 = (Button) findViewById(R.id.button8);
    button8.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (btnBtAccess.getText() == "Close") {     
                move = -100;
                try {
                        RoboController.outputStream.write('B');
                        RoboController.outputStream.write((int)(-move));
                        RoboController.outputStream.write('\r');
                        RoboController.outputStream.write('\n');
                        throtleText.setText("B "+String.valueOf((int)(-move)+" cm/s"));
                        progressBarY.setProgress(100+ move);
                    } catch (IOException e) {
                        btStatus.setText(" > Error sending data!");
                    }

            }

        }
    });

    //************************************************************************************
    button9 = (Button) findViewById(R.id.button9);
    button9.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (btnBtAccess.getText() == "Close") {     
                steer = 30;
                try {
                    int calDirection = (int)((steer - 30) - calValue);
                        RoboController.outputStream.write('L');
                        RoboController.outputStream.write(calDirection);
                        RoboController.outputStream.write('\r');
                        RoboController.outputStream.write('\n');
                        steeringText.setText("L "+String.valueOf((int)(steer - 30)+" deg"));
                        steer1 = (steer * 334) / 100;
                        progressBarX.setProgress(steer1);
                    } catch (IOException e) {
                        btStatus.setText(" > Error sending data!");
                    }

            }

        }
    });

    //**********************************************************************************
    // imageTouchPadHorz=( ImageView )findViewById(R.id.imageTouchPadHorz);
    // imageTouchPadHorz.setOnTouchListener(new View.OnTouchListener(){







  //**************************************************************************************************  


    ((SensorManager)getSystemService(Context.SENSOR_SERVICE)).registerListener(
             new SensorEventListener() {    
                @Override  
                public void onSensorChanged(SensorEvent event) {  


                    if(button1.getText() == "Tilt Steer Enabled" && btnBtAccess.getText() == "Close")
                    {


                        if(axisSelector == 0)
                        {
                            xPos2 = -event.values[0] * 3;
                            xPos2 = ((xPos2 * 100) / 10) / 3; 
                            axis1 = xPos2;
                            yPos2 = -event.values[1];
                            yPos2 = yPos2 * 10; 
                            axis = yPos2;
                        }

                        else
                        {
                            xPos2 = -event.values[0] * 3;
                            xPos2 = ((xPos2 * 100) / 10) / 3; 
                            axis = xPos2;
                            yPos2 = -event.values[1];
                            yPos2 = yPos2 * 10; 
                            axis1 = yPos2;

                        }
                        if(axis > 30)
                            axis = 30;
                        if(axis < -30)
                            axis = -30;
                        if(axis < 0)
                        {
                            try
                            {
                                int calDirection2 = (int)(-axis);
                                RoboController.outputStream.write('L');
                                RoboController.outputStream.write(calDirection2);
                                RoboController.outputStream.write('\r');
                                RoboController.outputStream.write('\n');
                                steeringText.setText("L "+String.valueOf((int)(-axis)+" deg"));
                                int xPos3 = (int)axis;
                                xPos3 = 100 + ((xPos3 * 334) / 100);
                                progressBarX.setProgress(xPos3);

                            }   catch (IOException e) {
                                btStatus.setText(" > Error sending data!");
                            }
                        }

                        else
                        {
                            try
                        {
                            int calDirection2 = (int)(axis);
                            RoboController.outputStream.write('R');
                            RoboController.outputStream.write(calDirection2);
                            RoboController.outputStream.write('\r');
                            RoboController.outputStream.write('\n');
                            steeringText.setText("R "+String.valueOf((int)(axis)+" deg"));
                            int xPos3 = (int)axis;
                            xPos3 = ((xPos3 * 334) / 100) + 100;
                            progressBarX.setProgress(xPos3);
                        }   catch (IOException e) {
                            btStatus.setText(" > Error sending data!");
                        }

                        }

                        if(axis1 > 50)
                            axis1 = 50;
                        if(axis1 < -50)
                            axis1 = -50;
                        if(axis1 > 0)
                        {
                            try {
                                RoboController.outputStream.write('F');
                                RoboController.outputStream.write((int)(axis1 * 2));
                                RoboController.outputStream.write('\r');
                                RoboController.outputStream.write('\n');
                                //throtleText.setText("F " + String.valueOf((int)(100-(axis1 * 2))) + " cm/s");
                                throtleText.setText("F " + String.valueOf((int)(axis1 * 2)) + " cm/s");
                                int yPos3 = (int)axis1;
                                yPos3 = yPos3 * 2;
                                progressBarY.setProgress(100+yPos3);
                            } catch (IOException e) {
                                btStatus.setText(" > Error sending data!");
                            }
                        }

                        if(axis1 < 0)
                        {
                            try {
                                RoboController.outputStream.write('B');
                                RoboController.outputStream.write((int)(-axis1 * 2));
                                RoboController.outputStream.write('\r');
                                RoboController.outputStream.write('\n');
                                throtleText.setText("B " + String.valueOf((int)(-axis1 * 2)) + " cm/s");
                                int yPos3 = (int)axis1;
                                yPos3 = yPos3 * 2;
                                progressBarY.setProgress(100+yPos3);
                            } catch (IOException e) {
                                btStatus.setText(" > Error sending data!");
                            }
                        }

               }
               }
                @Override  
                public void onAccuracyChanged(Sensor sensor, int accuracy) {} //ignore
            },
            ((SensorManager)getSystemService(Context.SENSOR_SERVICE))
            .getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),   
             SensorManager.SENSOR_DELAY_NORMAL);


    //*************************************************************************************************************************
}



@Override
public boolean onTouch(View v, MotionEvent event) {
    int xPos, yPos, action, actionCode;

    if (btnBtAccess.getText() == "Close") {         
        xPos = 0;
        yPos = 0; 
        for (int i = 0; i < event.getPointerCount(); i++) {
            xPos = (int)event.getX(i);
            yPos = (int)event.getY(i);


            if (xPos < 200) { 
                //switch(v.getId()){
                //case R.id.imageTouchPadHorz://original code(   
            //if (xPos > 0 ) {                                          //testing purpose not needed
                if (yPos > minY && yPos < maxY) {                           //original code (edited) 275 - 325
                //if(yPos >0 ){                                         //testing purpose not needed
                    //xPos -= 5;                                        //whats the purpose
                    if (xPos > 150)                                     //original is 200
                        xPos = 150;                                     //original is 200
                    else if (xPos < 2)
                        xPos = 0;

                    if (xPos < 75) {    
                        if(selector % 2 == 1){
                        //original is 100
                        //int calDirection..... is to calculate value for pcb
                        //RoboController.outputStream.write is to write value on LCD
                        //steering text is to display on android device
                        //progressBarX is to fill the bar with yellow colour
                        try {
                            int calDirection = (int)((75-xPos)/2.4 - calValue);                         //100 , 3.3
                            if (calDirection < 0)
                                calDirection = 0;
                            RoboController.outputStream.write('L');
                            RoboController.outputStream.write(calDirection);
                            RoboController.outputStream.write('\r');
                            RoboController.outputStream.write('\n');
                            steeringText.setText("L "+String.valueOf((int)((75-xPos)/2.4))+" deg");     //100 , 3.3
                            //extra calculation to completely fill the bar
                            //due to the change of value from 200 -> 150, 100 -> 75
                            //used *133/100 as xPos and progressbar is int
                            //1.33 as 75*1.33 and 150*1.33 is almost = 100, 200
                            xPos = (xPos*133)/100;                      
                            progressBarX.setProgress(xPos);
                        } catch (IOException e) {
                            btStatus.setText(" > Error sending data!");
                        }
                    }
                    }
                    else {
                        if(selector % 2 == 1){
                        //all changes are same as above
                        try {
                            int calDirection = (int)((xPos-75)/2.42 + calValue);            
                            if (calDirection < 0)
                                calDirection = 0;
                            RoboController.outputStream.write('R');
                            RoboController.outputStream.write(calDirection);
                            RoboController.outputStream.write('\r');
                            RoboController.outputStream.write('\n');
                            steeringText.setText("R "+String.valueOf((int)((xPos-75)/2.42))+" deg");
                             xPos = (xPos * 133) / 100;
                            progressBarX.setProgress(xPos);
                        } catch (IOException e) {
                            btStatus.setText(" > Error sending data!");
                        }
                    }
                    }
                }
            }
    //      }


                    //values are calculated and changed to suit the UI of my device.
                    //original difference between the max value of height and min value was 200
                    // mine is 
            //******************************************************************************************
            else if (xPos > minX && xPos < maxX) {    //orignal is x > 900    set the limit length of scroll 600-700
                    //i used 220 - > 350, but the increase in limit is for clearance 
                if (yPos > limitY && yPos < limitY2) {                  //original is y > 100   set the limit height of scroll
                    action = event.getAction();
                    actionCode = action & MotionEvent.ACTION_MASK;
                    if (actionCode == MotionEvent.ACTION_POINTER_UP || actionCode == MotionEvent.ACTION_UP)
                    {
                        yPos = 285;                 //285 is the center of scrollbar    //original is 420
                        //yPos = yPos - 185;
                    }
                    else
                    {
                        if (yPos > 350)             //original is y > 520
                        {
                            yPos = 350;             //min value of scroll bar
                            //yPos = yPos - 150;
                        }
                        else if (yPos < 220)        //original is y < 320
                        {
                            yPos = 220;             //max value of scroll bar
                            //yPos = 220 - 220;
                        }
                    }
                    //purpose of 1.5405 is when scrollbar value is 285, difference of 285*1.5405 - 339 must be at least
                    //or equals to 100 yet it cannot be too big. 99.9 or 100.1 will affect the max and min value of cm/s
                    //the maximum difference is made as 200 so as to make life easier when calculating and displaying
                    //the value on screen and the progressbar. 
                    //best to use simple multiplication factors of 200
                    yPos = ((yPos * 15405)/10000) - 339;     
                    if (yPos < 100) {
                        if(yPos < 1)            //this if is used to prevent forward 101 from coming out
                            yPos = 0;
                        try {
                            RoboController.outputStream.write('F');
                            RoboController.outputStream.write((int)(100-yPos));
                            RoboController.outputStream.write('\r');
                            RoboController.outputStream.write('\n');
                            throtleText.setText("F " + String.valueOf((int)(100-yPos)) + " cm/s");
                            progressBarY.setProgress(200-yPos);
                        } catch (IOException e) {
                            btStatus.setText(" > Error sending data!");
                        }
                    }
                    else {
                        try {
                            RoboController.outputStream.write('B');
                            RoboController.outputStream.write((int)(yPos-100));
                            RoboController.outputStream.write('\r');
                            RoboController.outputStream.write('\n');
                            throtleText.setText("B " + String.valueOf((int)(yPos-100)) + " cm/s");
                            progressBarY.setProgress(200-yPos);
                        } catch (IOException e) {
                            btStatus.setText(" > Error sending data!");
                        }
                    }
                }
            }
        }

    }

    return true;
}


//**********************************************************************************************************![xlarge image][1]

xlarge image

normal

enter image description here

1 个答案:

答案 0 :(得分:0)

听起来你引用的代码中的元素可能不在x-large之外的布局中。检查所有元素及其ID。