0

I have already declared the R into a variable. However, the error still remains there. I cleaned the project and restarted it but nothing changed. Im a beginner to eclipse android coding. Can someone help me out with the R variable?

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>
8
  • 1
    check if there is any errors in resource files Commented Oct 24, 2013 at 4:45
  • Update your sdk tools to the latest version too. Commented Oct 24, 2013 at 4:47
  • You are using the names of your class varialbes: Button btnSendSMS; R.id.btnSendSMS Did you also give that button the same id name in the xml file, like: android:id="@+id/btnSendSMS" Commented Oct 24, 2013 at 4:49
  • Clean your project & Restart your Eclipse. if the problem still exist, then update your SDK tools Commented Oct 24, 2013 at 4:50
  • R error usually occurs if there is something wrong in your resource file, usually some mistake in the xml files. Check each file thoroughly and you will find the mistake. Then just clean the project and you are good to go. Commented Oct 24, 2013 at 4:51

4 Answers 4

3

Remove line import com.example.smsmessaging.R; We should not import it in our .java.

Sign up to request clarification or add additional context in comments.

8 Comments

why is that you should not import
the R is still not a variable after removing it.
you clean the project which will rebuild your resource(R) file and then you don't need to import R.
Thank you for the info. I just removed the R import. However, the main cannot be resolved now:( Do i need to include anything in my xml file?
|
1

Once check your imports and remove unused import in your code:
This line: import com.example.smsmessaging.R;
Then organize imports -> Ctrl+Shift+O

Thats it...

4 Comments

i've removed the line, but the R is still not resolved into a variable.
and also check your all xml files is there any errors in it.May it'll solve your issue.
Close all files, clean project, restart Eclipse.
I did it again. The R is resolved into a variable. but the main isn't. And "public class SMS extends Activity" the SMS error states that the public type must be defined :((
0

First Remove tour R.java file form gen folder. Now clean your project and import android.R;

Alternate import can be done by pressing Ctrl+Shift+O

1 Comment

R can be resolved now. But the main isn't. And "public class SMS extends Activity" the SMS error states that the public type must be defined :((
0

You shouldn't import R or define it as a variable. Quoting from http://source.android.com/source/using-eclipse.html:

Note: Eclipse sometimes likes to add an import android.R statement at the top of your files > that use resources, especially when you ask eclipse to sort or otherwise manage imports. > This will cause your make to break. Look out for these erroneous import statements and delete them.

If there is no error in your resource xml files, then R should automatically be available. XML issues are usually reported in Eclipse. If you're not able to figure out the error, post your xml files here.

6 Comments

Yes, the reason for that would be that there was an error in one of the resource xml sheets. If you could post those, someone here might be able to better comment on it.
Hold on; main isn't resolved? What are you referring to as "main"? Can you give the exact error?
Well, I'm almost sure you meant R.layout.main. If that is the case, then everyone would be interested in that xml.
Yes! the R.layout.main! After removing the R import, the main is stated as "cannot be resolved into a variable"
So in that case post that layout's xml here!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.