生成和运行任务未显示任何错误,但应用程序未启动

时间:2019-06-29 10:42:09

标签: 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, 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;
}

没有显示任何错误,但是当我在移动设备上打开应用程序时,它表明该应用程序已停止工作

0 个答案:

没有答案