Android Studio: недопустимый запуск типа - PullRequest
0 голосов
/ 27 января 2020

Когда я строю свой проект, появляется сообщение об ошибке "Недопустимый запуск типа".

Я предполагаю, что строка, которая создает проблему, является последней Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

Все работает, за исключением этого

Спасибо

MainActivity

package com.example.pilotage;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    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 mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStatusBlueTV = findViewById(R.id.statusBluetoothTv);
        mPairedTV = findViewById(R.id.pairedTv);
        mBlueIv = findViewById(R.id.bluetoothIv);
        mONBtn = findViewById(R.id.onBtn);
        mOffBtn = findViewById(R.id.offBtn);
        mDiscoverBtn = findViewById(R.id.discoverableBtn);
        mPairedBtn = findViewById(R.id.pairedBtn);

        //adapteur
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        //vérifier si le bluetooth est activé ou non
        if(mBluetoothAdapter == null) {
            mStatusBlueTV.setText("Le Bluetooth n'est pas activé");
        }
        else {
            mStatusBlueTV.setText("Le Bluetooth est activé");
        }

        //accorder l'image selon si le bluetooth est activé ou non
        if(mBluetoothAdapter.isEnabled()){
            mBlueIv.setImageResource(R.drawable.ic_action_on);
        }
        else {
            mBlueIv.setImageResource(R.drawable.ic_action_off);
        }

        //Activer le bluetooth via l'appui sur le bouton
        mONBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isEnabled()){
                    Toast.makeText(getApplicationContext(), "Mise en route du Bluetooth...", Toast.LENGTH_SHORT).show();
                    //intent to on bluetooth
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(intent, REQUEST_ENABLE_BT);
                }
                else {
                    Toast.makeText(getApplicationContext(), "Le bluetooth est déjà activé.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //discover bluetooth btn click
        mDiscoverBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isDiscovering()){
                    Toast.makeText(getApplicationContext(), "Votre apapreil est devenu visible par les autres ", Toast.LENGTH_SHORT).show();
                    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 (mBluetoothAdapter.isEnabled()){
                    mBluetoothAdapter.disable();
                    Toast.makeText(getApplicationContext(), "Désactivation du Bluetooth...", Toast.LENGTH_SHORT).show();
                    mBlueIv.setImageResource(R.drawable.ic_action_off);
                }
                else {
                    Toast.makeText(getApplicationContext(), "Le Bluetooth est déjà désactivé", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //get paired devices btn click
        mPairedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBluetoothAdapter.isEnabled()){
                    mPairedTV.setText("Paired Devices");
                    Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
                    for (BluetoothDevice device: devices){
                        mPairedTV.append("\nDevice: " + device.getName()+ ", " + device);
                    }
                }
                else {
                    //bluetooth is off so can't get paired devices
                    Toast.makeText(getApplicationContext(), "Activez le bluetooth pour obtenir des appareils", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                switch (requestCode){
                    case REQUEST_ENABLE_BT:
                        if (resultCode == RESULT_OK){
                            //bluetooth is on
                            mBlueIv.setImageResource(R.drawable.ic_action_on);
                            showToast("Le Bluetooth est activé");
                        }
                        else {
                            //user denied to turn bluetooth on
                            showToast("Le Bluetooth ne peut pas être mit en route");
                        }
                        break;
                }
                MainActivity.super.onActivityResult(requestCode, resultCode, data);
            }

            //toast message function
            private void showToast(String msg){
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
            }
        });

1 Ответ

0 голосов
/ 27 января 2020

Ваш код содержит пропущенные braces {} и неуместные parenthesis ().

Вместо правильного закрытия mPairedBtn.setOnClickListener(new View.OnClickListener() {});, вы включили onActivityResult и showToast в onClickListener. Что приводит к пропавшим onCreate и Class braces {}.

package com.example.pilotage;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    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 mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStatusBlueTV = findViewById(R.id.statusBluetoothTv);
        mPairedTV = findViewById(R.id.pairedTv);
        mBlueIv = findViewById(R.id.bluetoothIv);
        mONBtn = findViewById(R.id.onBtn);
        mOffBtn = findViewById(R.id.offBtn);
        mDiscoverBtn = findViewById(R.id.discoverableBtn);
        mPairedBtn = findViewById(R.id.pairedBtn);

        //adapteur
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        //vérifier si le bluetooth est activé ou non
        if (mBluetoothAdapter == null) {
            mStatusBlueTV.setText("Le Bluetooth n'est pas activé");
        } else {
            mStatusBlueTV.setText("Le Bluetooth est activé");
        }

        //accorder l'image selon si le bluetooth est activé ou non
        if (mBluetoothAdapter.isEnabled()) {
            mBlueIv.setImageResource(R.drawable.ic_action_on);
        } else {
            mBlueIv.setImageResource(R.drawable.ic_action_off);
        }

        //Activer le bluetooth via l'appui sur le bouton
        mONBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isEnabled()) {
                    Toast.makeText(getApplicationContext(), "Mise en route du Bluetooth...", Toast.LENGTH_SHORT).show();
                    //intent to on bluetooth
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(intent, REQUEST_ENABLE_BT);
                } else {
                    Toast.makeText(getApplicationContext(), "Le bluetooth est déjà activé.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //discover bluetooth btn click
        mDiscoverBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBluetoothAdapter.isDiscovering()) {
                    Toast.makeText(getApplicationContext(), "Votre apapreil est devenu visible par les autres ", Toast.LENGTH_SHORT).show();
                    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 (mBluetoothAdapter.isEnabled()) {
                    mBluetoothAdapter.disable();
                    Toast.makeText(getApplicationContext(), "Désactivation du Bluetooth...", Toast.LENGTH_SHORT).show();
                    mBlueIv.setImageResource(R.drawable.ic_action_off);
                } else {
                    Toast.makeText(getApplicationContext(), "Le Bluetooth est déjà désactivé", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //get paired devices btn click
        mPairedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBluetoothAdapter.isEnabled()) {
                    mPairedTV.setText("Paired Devices");
                    Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
                    for (BluetoothDevice device : devices) {
                        mPairedTV.append("\nDevice: " + device.getName() + ", " + device);
                    }
                } else {
                    //bluetooth is off so can't get paired devices
                    Toast.makeText(getApplicationContext(), "Activez le bluetooth pour obtenir des appareils", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_ENABLE_BT:
                if (resultCode == RESULT_OK) {
                    //bluetooth is on
                    mBlueIv.setImageResource(R.drawable.ic_action_on);
                    showToast("Le Bluetooth est activé");
                } else {
                    //user denied to turn bluetooth on
                    showToast("Le Bluetooth ne peut pas être mit en route");
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //toast message function
    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...