Reputation: 1655
I am working on an android project from past one month. I am running the sdk in 64-bit windows 8 pc. I keep encountering the error which states that " R cannot be resolved to a variable". I have tried several solutions which have been posted in the web, some of them are:
Cleaning the project and building again.
Removing errors from XML file ( no such error is observed in my project)
Change of the package name ( which was not at all done in my case)
Import android.R ( this is not imported in my project)
Installing latest updates in sdk (all are installed)
This error occurs mostly when I clean the project or build it. The project that i was working on was running till yesterday till I cleaned the project. Building the project is also not solving the problem. In the mean while I was testing code of passing values from android to php to store data in mysql. This was a test code which I had acquired from a website. This code is also showing the same error.
The due date for my project is nearing and I dont know how to solve this error.
Code:
MainActivity.java
package com.example.sdvd;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//final EditText eid=(EditText) findViewById(R.id.e1);
final EditText e_id=(EditText) findViewById(R.id.e1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
Button insert=(Button) findViewById(R.id.button1);
Toast.makeText(this, "Yo", Toast.LENGTH_SHORT);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
id = e_id.getText().toString();
name = e_name.getText().toString();
insert();
}
});
}
public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Id"
android:padding="11dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:ems="10"
android:inputType="number">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="11dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Insert" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/e1"
android:layout_below="@+id/e1"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="11dp" />
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="UTF-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sdvd"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.sdvd.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 3
Views: 215
Reputation: 48871
In activity_main.xml delete the following two lines from your first EditText
...
<requestFocus />
</EditText>
Then change the last line of it to have a closing /
as follows...
android:inputType="number" />
...then clean / rebuild your project.
EDIT: OK, wait...the R.java file you show in your answer has a package name as follows...
package com.example.test;
...but the package name of your Activity
is...
package com.example.sdvd;
...your Activity
is never going to find the R.java file in that case as the two have to be in the same package or you have to explicitly import com.example.test.R
in your Activity
.
Sort out your package names and get them in line with each other and everything should work.
BTW - for reference here on stackoverflow don't post an answer to your own question which doesn't actually answer the problem - it confuses the situation and makes it harder for people to follow what you're asking - just edit your original question if you need to add extra information.
Upvotes: 2
Reputation: 1655
This method solved the R file problem. I just copied the code into a new project . I didn't change anything. The 'R cannot be resolved to a variable' error disappeared magically after I build the project. This method has worked for me a lot of times. But don't you guys think it is a waste of time to copy everything into a new project every time this error occurs ( what if the project is really big). Shouldn't there be a proper solution. How can the code if just copied from one file to another project remove such error.
Although the R file error was removed. A new error is being shown i.e. 'e1 cannot be resolved or is not a field' where e1 is a edittext field which is present in the XML file.
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Id"
android:padding="11dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:ems="10"
android:inputType="number"/>
I am using the following code to find e1
final EditText e_id=(EditText) findViewById(R.id.e1);
My R File
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.example.test;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080001;
public static final int button1=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f070000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
Last time the same error occurred and i cleaned the project. From that moment the R file error was shown. Is there a proper way to debug this error?
Just noticed that my Edit Text is not getting added automatically in the id section of R File
Upvotes: 0
Reputation: 5239
I think there is a problem with ending EditText in your xml file.
Try this out..
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Id"
android:padding="11dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:ems="10"
android:inputType="number"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="11dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Insert" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/e1"
android:layout_below="@+id/e1"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="11dp" />
</RelativeLayout>
Upvotes: 0