R cannot be resolved by a variable [could not be solved using the solutions given in the forums)

I am working on an Android project from last month. I am running sdk on 64 bit windows 8 pc. I continue to encounter an error that states that "R cannot be resolved by a variable." I tried several solutions that were published on the Internet, some of them:

  • Clean up the project and create again.

  • Removing errors from an XML file (such an error is not observed in my project)

  • Changing the package name (which in my case was not done at all)

  • Import android.R (this is not imported into my project)

  • Install the latest updates in sdk (all installed)

This error occurs mainly when cleaning up a project or creating it. The project I was working on worked until yesterday, until I cleaned up the project. Building a project also does not solve the problem. While I tested the code for passing values ​​from android to php to store data in mysql. This was the test code I received from the website. This code also shows the same error.

The deadline for my project is close, and I do not know how to solve this error.

the 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>
+3
source share
3 answers

In activity_main.xml, remove the following two lines from your first EditText...

    <requestFocus />
</EditText>

Then change the last line to close /as follows:

android:inputType="number" />

... then clean / rebuild the project.

EDIT: , ... R.java, , :

package com.example.test;

... Activity...

package com.example.sdvd;

... Activity R.java , com.example.test.R Activity.

, .

BTW - , stackoverflow, , - , , - , .

+2

R. . . "R " . . , , , , (, ). . , , ​​.

R . : "e1 ", e1 - edittext, XML.

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

, e1

final EditText e_id=(EditText) findViewById(R.id.e1);

R

/* 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;
    }
}

, . R. ?

, id R

0

I think there is a problem with the end of the EditText in your XML file.

Try it.

<?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>
0
source

All Articles