0

My mainactivity

package com.example.vivek.trackblue;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    BluetoothAdapter  bt = BluetoothAdapter.getDefaultAdapter();
    Context context = getApplicationContext();
    CharSequence text = "Bluetooth is not supported";
    int duration = Toast.LENGTH_SHORT;
    Toast toast1 = Toast.makeText(context, text, duration);

    Button b1 = (Button)findViewById(R.id.button);
    private final static int  REQUEST_ENABLE_BT=1;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if(bt == null){
                    toast1.show();
                }
                if(bt.isEnabled())
                {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                }
            }
        });

    }
}

and this is my AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.vivek.trackblue">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

I am Trying to create an application where the user would press a button to start Bluetooth.

My code shows no errors on the ide but when it gets installed in the device, it crashes with an error "Unfortunately TrackBlue stopped working". Here trackblue is my application's name.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

3

Simply because you have assigned value to button variable before setting content view for the screen.

Error is because it tries to find the view by the given button's ID even before it is set on to screen.

Solution: Write the findViewById() line after setContentView() inside onCreate()

Sandesh Baliga
  • 579
  • 5
  • 20