Unable to resolve character R

I worked on Android Studio to add a short message for toasts (I was making an Android Wear app)

I could not know why this code has an error in the character R. It says: "Unable to resolve character R".

    import android.app.Activity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.wearable.view.WatchViewStub;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MyActivity extends Activity {

        private TextView mTextView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);

    //getnextpage
    Button getnextpage;
    getnextpage = (Button) findViewById(R.id.getnextpage);
    getnextpage.setOnClickListener(new OnclickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Here is 2nd Page", Toast.LENGTH_LONG).show();
        }
    });
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
  }
 }
+4
source share
7 answers

Some procedures are described below:

1) Try to complete the project BuildClean/Rebuild.

2) Manually enter import <package name>.RIf there is no import R in import.

3) complete all resources of the corresponding file XML.

I hope that any of them will be useful for you, since there is no concrete way to solve this problem.

+8

R , R .

import <packagename>.R;

<packagename> .

R , Android Studio , .

+3

, , , , " " Android Studio, :

: Android

"Configure", AndroidManifest.xml ( "android" ), "R" , .

- , , : "clean", "rebuild" Android- - .

0

, Gradle . , Gradle .

0

, build.gradle.

compiledSdkVersion appcompat.

compileSdkVersion 22, appcompat

compile 'com.android.support:appcompat-v7:22.2.1'

, , .

0

, , , , !

"R" , , R - ( build\generated\source\r\debug\com\domain.projectname\R). R , , /, R ( ). :

1) , import android.R; . R. , "", "", : import com.example.project.R

2) R - . , , , , . .

3) , gradle . !

4) , R . , . , , , R , . Build Clean project, , , Rebuild project. R .

P.S:

0
source

This happens if there is no link in your class R.Classthat you may have to import. So, before running the class, you can just start with

import Your_Package_name.R;

eg.

import com.example.myproject.R;

You can manually write this when the project is not a solution. Clean/Rebuild

Usage example:

import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import com.example.jsonwebservice.R;

It worked for me.

0
source

All Articles