【问题标题】:R cannot be resolved into a variable after declaration声明后R无法解析为变量
【发布时间】:2013-10-24 04:45:07
【问题描述】:

我已经将 R 声明为一个变量。但是,错误仍然存​​在。我清理了项目并重新启动它,但没有任何改变。我是 Eclipse android 编码的初学者。有人可以帮我解决 R 变量吗?

Main.java

package com.example.smsmessaging;
import com.example.smsmessaging.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.telephony.gsm.SmsManager;


public class SMS extends Activity 
{

 Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {                
            String phoneNo = txtPhoneNo.getText().toString();
            String message = txtMessage.getText().toString();                 
            if (phoneNo.length()>0 && message.length()>0)                
                sendSMS(phoneNo, message);                
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}    
public class SMS extends Activity 
{
    //...

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

    //---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }    
  }
  //---sends an SMS message to another device---
  private void sendSMS(String phoneNumber, String message)
  {         
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
   }

xml:

    <receiver android:name=".SmsReceiver"> 
        <intent-filter> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>

</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>

【问题讨论】:

  • 检查资源文件是否有错误
  • 也将您的 sdk 工具更新到最新版本。
  • 您正在使用您的类变量的名称: Button btnSendSMS; R.id.btnSendSMS 你是否也在 xml 文件中为该按钮提供了相同的 id 名称,例如:android:id="@+id/btnSendSMS"
  • 清理您的项目并重新启动您的 Eclipse。如果问题仍然存在,请更新您的 SDK 工具
  • R 错误通常发生在您的资源文件有问题时,通常是 xml 文件中的一些错误。彻底检查每个文件,你会发现错误。然后只需清理项目,您就可以开始了。

标签: android eclipse


【解决方案1】:

删除行 import com.example.smsmessaging.R;我们不应该将它导入到我们的 .java 中。

【讨论】:

  • 为什么不应该导入
  • R 去掉后仍然不是变量。
  • 您清理将重建您的资源(R)文件的项目,然后您不需要导入 R。
  • 感谢您提供的信息。我刚刚删除了 R 导入。但是,现在主要无法解决:(我需要在我的xml文件中包含任何内容吗?
【解决方案2】:

检查您的导入并删除代码中未使用的导入后:
此行:import com.example.smsmessaging.R;
然后组织导入 -> Ctrl+Shift+O

就是这样...

【讨论】:

  • 我已删除该行,但 R 仍未解析为变量。
  • 并检查您的所有 xml 文件是否有任何错误。也许它会解决您的问题。
  • 关闭所有文件,清理项目,重启Eclipse。
  • 我又做了一次。 R 被解析为一个变量。但主要不是。并且“公共类 SMS 扩展 Activity”SMS 错误指出必须定义公共类型 :((
【解决方案3】:

首先从 gen 文件夹中删除 tour R.java 文件。现在清理你的项目并导入 android.R;

可以通过按 Ctrl+Shift+O 来完成备用导入

【讨论】:

  • R 现在可以解决了。但主要不是。并且“公共类 SMS 扩展 Activity”SMS 错误指出必须定义公共类型 :((
【解决方案4】:

您不应导入 R 或将其定义为变量。 引用http://source.android.com/source/using-eclipse.html:

注意:Eclipse 有时喜欢在使用资源的文件顶部添加 import android.R 语句,尤其是当您要求 eclipse 对导入进行排序或以其他方式管理时。 > 这将导致您的制造中断。注意这些错误的导入语句并删除它们。

如果您的资源 xml 文件中没有错误,则 R 应该自动可用。 XML 问题通常在 Eclipse 中报告。如果您无法找出错误,请在此处发布您的 xml 文件。

【讨论】:

  • 是的,原因是其中一个资源 xml 表中存在错误。如果您可以发布这些内容,这里的人也许可以更好地评论它。
  • 等一下;主要没有解决?你所说的“主要”是什么?你能给出确切的错误吗?
  • 嗯,我几乎可以肯定你的意思是 R.layout.main。如果是这样,那么每个人都会对那个 xml 感兴趣。
  • 是的! R.layout.main!去掉R导入后,main声明为“不能解析成变量”
  • 所以在这种情况下,请在此处发布该布局的 xml!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多