Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
此代码工作不正常。它说android应用程序意外关闭_Android - Fatal编程技术网

此代码工作不正常。它说android应用程序意外关闭

此代码工作不正常。它说android应用程序意外关闭,android,Android,检查此代码 此代码工作不正常。它说android应用程序意外关闭。 尝试在此处使用bluetoothadapter类,但不在设备上工作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //On = (Button)findViewById(R.id

检查此代码 此代码工作不正常。它说android应用程序意外关闭。 尝试在此处使用bluetoothadapter类,但不在设备上工作

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   //On = (Button)findViewById(R.id.button1);


}

public void on(View view){
    BluetoothAdapter mblue = BluetoothAdapter.getDefaultAdapter(); 

    if(!mblue.isEnabled())
    {

    Context context = getApplicationContext();
    CharSequence text = "Bluetooth Enabled!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    }

    else{
        Context context = getApplicationContext();
        CharSequence text = "Already On!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

        }
 }

我试过你的代码,它是正确的。我很确定您的问题与您没有向AndroidManifest.xml添加蓝牙权限有关。 只需将此行添加到清单:


从LogCat记录堆栈跟踪异常您是否为此设置了权限?否。。我是第一次尝试这一点,它似乎让我很困惑。我复制了另一个工作正常的代码,但当我自己实现它时,它就不工作了。您是否添加了AndroidManifest.xml?