Java 生成和运行任务未显示任何错误,但应用程序未启动
我制作了一个选项卡式布局应用程序,并在其中一个选项卡类中添加了蓝牙功能(打开/关闭、可发现、配对设备)。代码中未显示任何错误,但应用程序未启动Java 生成和运行任务未显示任何错误,但应用程序未启动,java,android,android-studio,android-fragments,bluetooth,Java,Android,Android Studio,Android Fragments,Bluetooth,我制作了一个选项卡式布局应用程序,并在其中一个选项卡类中添加了蓝牙功能(打开/关闭、可发现、配对设备)。代码中未显示任何错误,但应用程序未启动 public class Connect extends Fragment{ private static final int REQUEST_ENABLE_BT = 0; private static final int REQUEST_DISCOVER_BT = 1; TextView mStatusBlueTv, mPa
public class Connect extends Fragment{
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVER_BT = 1;
TextView mStatusBlueTv, mPairedTv;
ImageView mBlueIv;
Button mOnBtn, mOffBtn, mDiscoverBtn, mPairedBtn;
BluetoothAdapter mBlueAdapter;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK){
//bT IS ON
mBlueIv.setImageResource(R.drawable.ic_action_on);
//showToast("Bluetooth is on");
}
else {
//user denied to turn on
//showToast("Couldn't on Bluetooth");
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mStatusBlueTv = mStatusBlueTv.findViewById(R.id.statusBluetoothTv);
mPairedTv = mPairedTv.findViewById(R.id.pairedTv);
mBlueIv = mBlueIv.findViewById(R.id.bluetoothIv);
mOnBtn = mOnBtn.findViewById(R.id.onBtn);
mOffBtn = mOffBtn.findViewById(R.id.offBtn);
mDiscoverBtn = mDiscoverBtn.findViewById(R.id.discoverableBtn);
mPairedBtn = mPairedBtn.findViewById(R.id.pairedBtn);
//adapter
mBlueAdapter = BluetoothAdapter.getDefaultAdapter();
// check if bt is available
if (mBlueAdapter == null){
mStatusBlueTv.setText("Bluetooth is not available");
}
else mStatusBlueTv.setText("Bluetooth is available");
//set image acc to bt status on/off
if (mBlueAdapter.isEnabled()){
mBlueIv.setImageResource(R.drawable.ic_action_on);
}
else{
mBlueIv.setImageResource(R.drawable.ic_action_off);
}
//on btn click
mOnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBlueAdapter.isEnabled()){
//showToast("Turning on Bluetooth...");
//intent to on bt
Intent intent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}
else {
//showToast("Bluetooth is already on");
}
}
});
//discover bluetooth btn
mDiscoverBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mBlueAdapter.isDiscovering()){
//showToast("Making Your Device Discoverable");
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent,REQUEST_DISCOVER_BT);
}
}
});
//off btn click
mOffBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBlueAdapter.isEnabled()){
mBlueAdapter.disable();
//showToast("Turning Bluetooth Off");
mBlueIv.setImageResource((R.drawable.ic_action_off));
}
else {
//showToast("Bluetooth is already off");
}
}
});
//paired btn click
mPairedBtn.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBlueAdapter.isEnabled()) {
mPairedTv.setText("Paired Devices");
Set<BluetoothDevice> devices = mBlueAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
mPairedTv.append("\nDevices: " + device.getName() + "," + device);
}
} else {
//bt is off so can't get paired devices
// showToast("Turn on bluetooth to get paired devices");
}
}
}));}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.connect, container, false);
return root;
}
公共类连接扩展片段{
私有静态最终整数请求\u启用\u BT=0;
私有静态最终整数请求\u DISCOVER\u BT=1;
TextView mStatusBlueTv,mPairedTv;
ImageView mBlueIv;
按钮mOnBtn、mOffBtn、mDiscoverBtn、mPairedBtn;
蓝牙适配器;
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例请求\u启用\u BT:
if(resultCode==Activity.RESULT\u确定){
//英国电信正在播出
mBlueIv.setImageResource(R.drawable.ic_action_on);
//showToast(“蓝牙开启”);
}
否则{
//用户拒绝打开
//showToast(“无法使用蓝牙”);
}
打破
}
super.onActivityResult(请求代码、结果代码、数据);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mStatusBlueTv=mStatusBlueTv.findviewbyd(R.id.statusBluetoothTv);
mPairedTv=mPairedTv.findViewById(R.id.pairedTv);
mBlueIv=mBlueIv.findViewById(R.id.bluetoothIv);
mOnBtn=mOnBtn.findviewbyd(R.id.onBtn);
mOffBtn=mOffBtn.findViewById(R.id.offBtn);
MDisCorbTn=MDisCorbTn.findViewById(R.id.discoverableBtn);
mPairedBtn=mPairedBtn.findViewById(R.id.pairedBtn);
//适配器
mBlueAdapter=BluetoothAdapter.getDefaultAdapter();
//检查bt是否可用
if(mBlueAdapter==null){
mStatusBlueTv.setText(“蓝牙不可用”);
}
else mStatusBlueTv.setText(“蓝牙可用”);
//将图像acc设置为bt状态开/关
if(mBlueAdapter.isEnabled()){
mBlueIv.setImageResource(R.drawable.ic_action_on);
}
否则{
mBlueIv.setImageResource(R.drawable.ic_action_off);
}
//点击btn
mOnBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!mBlueAdapter.isEnabled()){
//showToast(“打开蓝牙…”);
//有意加入英国电信
意向意向=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(意图、请求和启用);
}
否则{
//showToast(“蓝牙已开启”);
}
}
});
//发现蓝牙btn
MDisCorbTn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!mBlueAdapter.isDiscovering()){
//showToast(“使您的设备可发现”);
意向意向=新意向(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE);
startActivityForResult(意图、请求、发现);
}
}
});
//关闭btn点击
mOffBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mBlueAdapter.isEnabled()){
mBlueAdapter.disable();
//showToast(“关闭蓝牙”);
mBlueIv.setImageResource((R.drawable.ic_action_off));
}
否则{
//showToast(“蓝牙已关闭”);
}
}
});
//成对btn点击
mPairedBtn.setOnClickListener((新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mBlueAdapter.isEnabled()){
mPairedTv.setText(“配对设备”);
Set devices=mBlueAdapter.getBondedDevices();
用于(蓝牙设备:设备){
mPairedTv.append(“\n设备:“+device.getName()+”,“+device”);
}
}否则{
//bt已关闭,因此无法获取配对设备
//showToast(“打开蓝牙以获得配对设备”);
}
}
}));}
@凌驾
公共视图onCreateView(
充气机、视图组容器、,
Bundle savedInstanceState){
视图根=充气机。充气(R.layout.connect,容器,错误);
返回根;
}
没有显示任何错误,但当我在手机上打开应用程序时,它会显示该应用程序已停止工作