2015-06-23 16 views
-1

有4個錯誤和四個警告。我沒有收到錯誤。請幫助。 順便說一句我是新來的android開發。Android R.即使刪除導入後也變紅並顯示錯誤android.r

這是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); 
     } 
    } 
} 

代碼也menu_main.xml的代碼是:

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

我已經嘗試了許多這裏給出,但couldn解決方案,T推出了什麼。

+0

你收到了什麼類型的錯誤? – sasikumar

+0

創建新項目自動添加java文件R.java將在您的項目中創建。 – Mano

+0

刪除導入com.technomentis.led_bluetooth.R;並導入android.R; – Mano

回答

0

刪除

import android.R;

and add

import your_package_name.R;

+0

它沒有幫助。 仍然存在三個錯誤錯誤:(50,44)誤差:無法找到符號變量btnOff befor,對於主要和btnOn –

0

問題出在您的import android.R;聲明。有時當你使用CTRL + SHIFT + O來組織導入時,eclipse會錯誤地添加import android.R;聲明。所以它在構建過程中隱藏了從代碼構建的R.java。

[編輯] 嘗試刪除該進口和添加import語句到你的R.java和清潔+版本。

好運。

+0

仍然存在三個錯誤 錯誤:(50,44)誤差:無法找到符號變量btnOff befor那主要和btnOn –

+0

@ ashok-suthar:檢查我的編輯。 – STaefi

+0

但是沒有R.java –

0

不會擺弄任何代碼,這個「R變紅」的問題是很常見的(並且都是關於gradle的),過去我做了很多,但是新的gradle和sdk現在少得多了,但對任何人有同樣的問題做如下希望它會爲你工作了,

這爲我工作:

  1. 關閉並重新打開項目
  2. 按「同步工程與搖籃文件」(與頂部菜單中的AVD管理器圖標 相鄰)

如果Gradle無法第一次同步,請重複1-2。 (我的第二次工作)。

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

相關問題