【问题标题】:Android R. getting red and showing error even after removing the import android.r即使在删除导入 android.r 后,Android R 也会变红并显示错误
【发布时间】:2015-06-23 05:32:27
【问题描述】:

有 4 个错误和四个警告。我没有收到错误。请帮忙。 顺便说一句,我是 android 开发的新手。

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

我已经尝试了这里给出的许多解决方案,但无法解决任何问题。

【问题讨论】:

  • 您收到了什么类型的错误?
  • 创建新项目 在那里自动添加java文件 R.java 将在你的项目中创建。
  • 删除导入com.technomentis.led_bluetooth.R;并导入android.R;来自您的项目
  • 如果您使用的是 eclipse,您可以在 problems 选项卡中看到错误。解决这个问题,它会再次起作用。
  • 1.无法解析符号 R

标签: java android r.java-file


【解决方案1】:

删除

导入android.R;

并添加

导入 your_package_name.R;

【讨论】:

  • 它没有帮助。仍然存在三个错误错误:(50、44)错误:在main和btnOn之前找不到符号变量btnOff
【解决方案2】:

问题在于您的import android.R; 语句。有时当你使用 CTRL + SHIFT + O 组织导入时,eclipse会错误地添加import android.R;语句。因此它隐藏了在构建过程中从您的代码构建的 R.java。

[编辑] 尝试删除该导入并将导入语句添加到您的 R.java 并清理 + 构建。

祝你好运。

【讨论】:

  • 仍然出现三个错误 Error:(50, 44) 错误:在main和btnOn之前找不到符号变量btnOff
  • 但是没有R.java
  • 始终 R.java 必须从您的项目代码构建。如果您的项目中没有 R.java(我对此表示怀疑),则没有常规方法可以解决该问题。您必须注释有错误的行,然后尝试再次清理构建项目以创建 R.java。之后,您应该再次添加这些资源以包含在您的 R.java 中。
  • setContentView(R.layout.main);错误:(47, 32) 错误:找不到符号变量 main btnOn = (Button) findViewById(R.id.btnOn);错误:(49, 43) 错误:找不到符号变量 btnOn btnOff = (Button) findViewById(R.id.btnOff);错误:(50, 44) 错误:找不到符号变量 btnOff
  • 可能是你的 Eclipse 和 SDK 的问题
【解决方案3】:

不要摆弄任何代码,这个“R 变红”的问题很常见(而且都与 gradle 有关),我过去经常遇到这种情况,但现在有了新的 gradle 和 sdk,这种情况要少得多,但是对于任何有相同问题的人,请按照以下方式操作,希望它也对您有用,

这对我有用:

  1. 关闭并重新打开项目
  2. 按“与 Gradle 文件同步项目”(在 AVD 管理器图标旁边 在顶部菜单中)

如果 Gradle 第一次无法同步,请重复 1-2。 (我的第二次工作)。

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

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2018-08-19
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多