-1

There are 4 nos of errors and four warning .I am not getting the errors. Pls Help. Btw i am new to android development.

This is the code of MyActivity.java

package com.technomentis.led_bluetooth;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import com.technomentis.led_bluetooth.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.R;

public class Led_bluetooth extends Activity {
    private static final String TAG = "LEDOnOff";

    Button btnOn, btnOff;

    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;

    // Well known SPP UUID
    private static final UUID MY_UUID =
            UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    // Insert your server's MAC address
    private static String address = "00:00:00:00:00:00";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "In onCreate()");

        setContentView(R.layout.main);

        btnOn = (Button) findViewById(R.id.btnOn);
        btnOff = (Button) findViewById(R.id.btnOff);

        btAdapter = BluetoothAdapter.getDefaultAdapter();
        checkBTState();

        btnOn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendData("1");
                Toast msg = Toast.makeText(getBaseContext(),
                        "You have clicked On", Toast.LENGTH_SHORT);
                msg.show();
            }
        });

        btnOff.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendData("0");
                Toast msg = Toast.makeText(getBaseContext(),
                        "You have clicked Off", Toast.LENGTH_SHORT);
                msg.show();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        Log.d(TAG, "...In onResume - Attempting client connect...");

        // Set up a pointer to the remote node using it's address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);

        // Two things are needed to make a connection:
        //   A MAC address, which we got above.
        //   A Service ID or UUID.  In this case we are using the
        //     UUID for SPP.
        try {
            btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        btAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(TAG, "...Connecting to Remote...");
        try {
            btSocket.connect();
            Log.d(TAG, "...Connection established and data link opened...");
        } catch (IOException e) {
            try {
                btSocket.close();
            } catch (IOException e2) {
                errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
            }
        }

        // Create a data stream so we can talk to server.
        Log.d(TAG, "...Creating Socket...");

        try {
            outStream = btSocket.getOutputStream();
        } catch (IOException e) {
            errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        Log.d(TAG, "...In onPause()...");

        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
                errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
            }
        }

        try     {
            btSocket.close();
        } catch (IOException e2) {
            errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
        }
    }

    private void checkBTState() {
        // Check for Bluetooth support and then check to make sure it is turned on

        // Emulator doesn't support Bluetooth and will return null
        if(btAdapter==null) {
            errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
        } else {
            if (btAdapter.isEnabled()) {
                Log.d(TAG, "...Bluetooth is enabled...");
            } else {
                //Prompt user to turn on Bluetooth
                Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }

    private void errorExit(String title, String message){
        Toast msg = Toast.makeText(getBaseContext(),
                title + " - " + message, Toast.LENGTH_SHORT);
        msg.show();
        finish();
    }

    private void sendData(String message) {
        byte[] msgBuffer = message.getBytes();

        Log.d(TAG, "...Sending data: " + message + "...");

        try {
            outStream.write(msgBuffer);
        } catch (IOException e) {
            String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
            if (address.equals("00:00:00:00:00:00"))
                msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
            msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

            errorExit("Fatal Error", msg);
        }
    }
}

Also the code of menu_main.xml is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

I have tried many solution given here but couldn,t come out with anything.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41

3 Answers3

0

Delete

import android.R;

and add

import your_package_name.R;

Sunny Garg
  • 1,073
  • 1
  • 6
  • 13
  • its not helping. still there are three errors Error:(50, 44) error: cannot find symbol variable btnOff befor that for main and btnOn – ashok suthar Jun 23 '15 at 06:23
0

The problem is with your import android.R; statement. Sometimes when you use CTRL + SHIFT + O to organize imports, eclipse adds import android.R; statement by mistake. So it hides the R.java built from your code during the build.

[EDIT] Try remove that import and add the import statement to your R.java and clean + build.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • still there are three errors Error:(50, 44) error: cannot find symbol variable btnOff befor that for main and btnOn – ashok suthar Jun 23 '15 at 06:09
  • but there is no R.java – ashok suthar Jun 23 '15 at 06:22
  • Always R.java must be built from your project codes. If there is no R.java in your project (I doubt that), there is not a routine way to solve that. You must comment the lines that have errors and then try to clean build the project again in order to create the R.java. After that you should add those resources again to be included in your R.java. – STaefi Jun 23 '15 at 06:28
  • setContentView(R.layout.main); Error:(47, 32) error: cannot find symbol variable main btnOn = (Button) findViewById(R.id.btnOn); Error:(49, 43) error: cannot find symbol variable btnOn btnOff = (Button) findViewById(R.id.btnOff); Error:(50, 44) error: cannot find symbol variable btnOff – ashok suthar Jun 23 '15 at 06:41
  • may be problem in your Eclipse and SDK – Android Dev Jun 23 '15 at 06:59
0

do not fiddle with any code , this "R getting red" issue is very common(and all is to do with gradle), I had it a lot in the past but with new gradle and sdk it is much less nowadays, but for anyone has the same issue do as follow hope it will work for you too,

This worked for me:

  1. Close and reopen project
  2. Press " Sync Project with Gradle file " (is next to AVD manager icon in top menu)

repeat 1-2 if Gradle could not Sync for first time. (mine worked in second time).

source, https://stackoverflow.com/a/31127514/4173238

Community
  • 1
  • 1
bastami82
  • 5,955
  • 7
  • 33
  • 44