安卓萌新一枚,一个bug,卡了好几天了,要崩溃了,求各路大仙救我

Protofrom 2017-05-08 10:57:57
一头雾水,eclipse没报错,但是运行就是直接停止运行,完全不知道该怎么办
...全文
914 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
酸豆角姑娘 2017-05-19
  • 打赏
  • 举报
回复
我也遇到过这种没有任何错误提示,就直接程序崩掉的情况。应该是.xml文件的布局出现了问题,如果不同的layout嵌套,查找一下是否会出现问题。改变一下控件书写的上下位置。
rainbowO_o 2017-05-18
  • 打赏
  • 举报
回复
看看你的xml 有没有写错了的
VNanyesheshou 2017-05-12
  • 打赏
  • 举报
回复
OnCheckedChangeListener listener=new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { if(_bluetooth.enable()){//打开蓝牙 Toast.makeText(MainActivity.this, "打开蓝牙中...", Toast.LENGTH_SHORT).show(); connect.setChecked(true); }else{ Toast.makeText(MainActivity.this, "打开失败...", Toast.LENGTH_SHORT).show(); connect.setChecked(false); } } else { _bluetooth.disable();// 关闭蓝牙 Toast.makeText(MainActivity.this, "关闭蓝牙中...", Toast.LENGTH_SHORT).show(); connect.setChecked(false); } } }; connect.setOnCheckedChangeListener(listener); 这里面有问题,connect.setChecked会回调onCheckedChanged,这个里面你又调用了connect.setChecked,应该会造成死循环。将onCheckedChanged中的connect.setChecked去掉,你再试试。
Protofrom 2017-05-10
  • 打赏
  • 举报
回复
而且只在手机上无法运行,模拟器正常运行
Protofrom 2017-05-10
  • 打赏
  • 举报
回复
引用 16 楼 chenandczh 的回复:
如果确定布局文件和代码都是没有问题的,先清理一下项目,再重新试。
我重建了个项目 一行一行带进去 然后能用了 以为绕过去了 清理了一下项目 又莫名其妙地终止了
绿领巾童鞋 2017-05-10
  • 打赏
  • 举报
回复
如果确定布局文件和代码都是没有问题的,先清理一下项目,再重新试。
_周星星 2017-05-10
  • 打赏
  • 举报
回复
activity 注册没额 断点调试 一行行的走三 用AS 推荐
line1213 2017-05-10
  • 打赏
  • 举报
回复
引用 13 楼 xiyi3926 的回复:
[quote=引用 12 楼 line1213 的回复:] 模拟器都没蓝牙,你怎么调
我在手机上试过了,现在问题是:一打开这个activity就停止了,根本就还没蓝牙什么事情[/quote] logcat按工程过滤下,看看报的啥错
Protofrom 2017-05-10
  • 打赏
  • 举报
回复
引用 12 楼 line1213 的回复:
模拟器都没蓝牙,你怎么调
我在手机上试过了,现在问题是:一打开这个activity就停止了,根本就还没蓝牙什么事情
line1213 2017-05-10
  • 打赏
  • 举报
回复
模拟器都没蓝牙,你怎么调
tangx10 2017-05-09
  • 打赏
  • 举报
回复
权限设置那有没有,注意一下
YXTS122 2017-05-09
  • 打赏
  • 举报
回复
代码不全,发全一点。。。。。。
frank.xiong 2017-05-09
  • 打赏
  • 举报
回复
没蓝牙直接finish了
头发还没秃a 2017-05-09
  • 打赏
  • 举报
回复
2楼说的是,要是没蓝牙或者蓝牙没打开的话就直接退出了
ESC尛蜜蜂 2017-05-09
  • 打赏
  • 举报
回复
你用的是模拟器么?模拟器没蓝牙 看代码没蓝牙就已经finish了
Protofrom 2017-05-09
  • 打赏
  • 举报
回复
package com.cy.smartcar;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.UUID;

import com.cy.smartcar.DeviceListActivity;
import com.cy.smartcar.MainActivity;
import com.cy.smartcar.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Switch;

public class MainActivity extends Activity implements OnClickListener {
	private final int REQUEST_CONNECT_DEVICE = 1;
	private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();// 获取本地蓝牙适配器
	static BluetoothSocket _socket = null; // 蓝牙通信socket
	BluetoothDevice _device = null; // 蓝牙设备
	boolean _discoveryFinished = false;
	boolean bRun = true;// 用于控制线程开关的变量
	boolean bThread = false;// 用于控制线程开关的变量
	private InputStream iss; // 输入流,用来接收蓝牙数据
	Calendar cal = Calendar.getInstance();
	final int DIALOG_TIME = 0; // 设置对话框id
	boolean sceneTrue = false;
	boolean offTrue = true;
	
	private final int REQUEST_SCENE = 2;
	private final static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB"; // SPP服务UUID号
	private String smsg = ""; // 显示用数据缓存
	private int smsgInt = 0;
	
	int speed;
	
	TextView tstate;
	TextView tspeed;
	
	TextView deviceName;
	Switch connect;
	Button search;
	Button up;
	Button down;
	Button tleft;
	Button tright;
	Button stop;
	Button fast;
	Button slow;
	Button exit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		//setContentView(R.layout.activity_main);
		System.out.println("----------------");
		// 如果打开本地蓝牙设备不成功,提示信息,结束程序
		
		if (_bluetooth == null) {
			Toast.makeText(this, "无法打开手机蓝牙,请确认手机是否有蓝牙功能!", Toast.LENGTH_LONG)
					.show();
			finish();
			return;
		}
		
		ButtonInit();
		/*
		if(_bluetooth.isEnabled()){
			connect.setChecked(true);
			Toast.makeText(this, "蓝牙已打开", Toast.LENGTH_LONG)
			.show();
		}else{
			connect.setChecked(false);
		}*/
	}

	/**
	 * 执行所有主界面控键对应的操作
	 */
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		System.out.println("========" + view.getId());
		switch (view.getId()) {
		// ==========================连接蓝牙设备==============================
		case R.id.search:
			if (_bluetooth.isEnabled() == false) {// 如果蓝牙服务不可用则提示
				Toast.makeText(this, "请打开蓝牙", Toast.LENGTH_SHORT).show();
				return;
			}
			connect.setChecked(true);
			// 如果未连接设备则打开DeviceListActivity进行设备搜索
			if (_socket == null) {
				Intent intent = new Intent(this, DeviceListActivity.class);
				startActivityForResult(intent, REQUEST_CONNECT_DEVICE);
				// startActivity(intent);
			} else {// 关闭连接socket
				try {
					iss.close();
					_socket.close();
					_socket = null;
					bRun = false;
					connect.setText("连接");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		// ==========================前进==============================
		case R.id.up:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(2);
					tstate.setText("小车状态:前进");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================后退==============================
		case R.id.down:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(4);
					tstate.setText("小车状态:后退");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================左转==============================
		case R.id.tleft:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(5);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================右转==============================
		case R.id.tright:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(7);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================刹车==============================
		case R.id.stop:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(8);
					tstate.setText("小车状态:停止");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================加速==============================
		case R.id.fast:
			sceneTrue = false;
			if (_socket != null) {
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(1);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;

		// ==========================减速==============================
		case R.id.slow:
			sceneTrue = false;
			if (_socket != null) {	
				try {
					OutputStream os = _socket.getOutputStream();
					os.write(3);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(this, "请先连接智能小车", Toast.LENGTH_SHORT).show();
			}
			break;
		// ==========================退出==============================
		case R.id.exit:
			finish();
			break;
		
		}
	}
	

	private void ButtonInit() {
		// TODO Auto-generated method stub
		deviceName = (TextView) this.findViewById(R.id.main_device_name);
		tstate = (TextView) this.findViewById(R.id.tstate);
		connect = (Switch) this.findViewById(R.id.connect);
		search = (Button) this.findViewById(R.id.search);
		up = (Button) this.findViewById(R.id.up);
		down = (Button) this.findViewById(R.id.down);
		tleft = (Button) this.findViewById(R.id.tleft);
		tright = (Button) this.findViewById(R.id.tright);
		stop = (Button) this.findViewById(R.id.stop);
		fast = (Button) this.findViewById(R.id.fast);
		slow = (Button) this.findViewById(R.id.slow);
		exit = (Button) this.findViewById(R.id.exit);

		// deviceName.setOnClickListener(this);
		//connect.setOnClickListener(this);
		OnCheckedChangeListener listener=new OnCheckedChangeListener() {  
            @Override  
            public void onCheckedChanged(CompoundButton buttonView,  
                    boolean isChecked) {  
                // TODO Auto-generated method stub  
                if (isChecked) {  
                    if(_bluetooth.enable()){//打开蓝牙
                    	Toast.makeText(MainActivity.this, "打开蓝牙中...", Toast.LENGTH_SHORT).show();
                    	connect.setChecked(true);
                    }else{
                    	Toast.makeText(MainActivity.this, "打开失败...", Toast.LENGTH_SHORT).show();
                    	connect.setChecked(false);
                    }
                } else {  
                    _bluetooth.disable();// 关闭蓝牙  
                    Toast.makeText(MainActivity.this, "关闭蓝牙中...", Toast.LENGTH_SHORT).show();
                    connect.setChecked(false);
                } 
            }  
        };  
		connect.setOnCheckedChangeListener(listener);
		search.setOnClickListener(this);
		up.setOnClickListener(this);
		down.setOnClickListener(this);
		tleft.setOnClickListener(this);
		tright.setOnClickListener(this);
		stop.setOnClickListener(this);
		fast.setOnClickListener(this);
		slow.setOnClickListener(this);
		exit.setOnClickListener(this);
	}
}
长度不够,底部一些确认无影响代码就没贴进来了。写的很烂,求轻喷
Protofrom 2017-05-09
  • 打赏
  • 举报
回复
引用 8 楼 tangx10 的回复:
权限设置那有没有,注意一下
权限有的呀
Protofrom 2017-05-09
  • 打赏
  • 举报
回复
引用 4 楼 a511341250 的回复:
你用的是模拟器么?模拟器没蓝牙 看代码没蓝牙就已经finish了
不知道为什么,模拟器运行的时候会跳过这段,就没有finish。
Protofrom 2017-05-08
  • 打赏
  • 举报
回复
问题似乎出在setContentView(R.layout.activity_main);里
Protofrom 2017-05-08
  • 打赏
  • 举报
回复
加载更多回复(1)

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧