0

When I used R.layout.activity in setContentView it says it cannot be resolved.

So, I tried importing it from R.layout but then it says

import R cannot be resolved.

package com.example.trial;
import com.example.trial.util.SystemUiHider;
import com.example.trial.R; 
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class Trial extends Activity{    
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    Button tut1 = (Button) findViewById(R.id.button1);
    tut1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        startActivity(new Intent(Trial.this, second.class));
        }
    });
}}
Sahil
  • 461
  • 1
  • 9
  • 24

2 Answers2

2

Remove:

import R.layout.activity

import android.R

If necessary add:

import your.package.R; //(shouldn't be needed though)

Do:

Clean & build

Explanation

When you import android.R you import the platform resources instead of your app resources.

TryinHard
  • 4,078
  • 3
  • 28
  • 54
JohanShogun
  • 2,956
  • 21
  • 30
  • i tried importing my own package but still no result... – Sahil Aug 20 '15 at 21:13
  • Update your code with how it looks now. – JohanShogun Aug 20 '15 at 21:14
  • after removing the line you mentioned another error appears where it fails to resolve the button i have used. – Sahil Aug 20 '15 at 21:15
  • If you used android.R for your button ID refer to it as android.R.button1, I.e. Use the fully qualified class name for android.R. Or change your xml file not to use Android.R, remember to clean and rebuild. – JohanShogun Aug 20 '15 at 21:16
  • k. if i import android.R is there no way making my "activity" avialable to it? – Sahil Aug 20 '15 at 21:26
  • Imports are just a short hand, you don't actually need to import anything you could always write the full class name. You can always refer to a class like this: package.name.ClassName, so you could refer to your own R com.example.trail.R and the android R android.R, in your code. – JohanShogun Aug 21 '15 at 05:20
  • May be, you got an error in your resource files (filename is wrong or resource not found). If you got the error, IDEA doesn't generate the R file – Bunyod Aug 21 '15 at 06:09
  • thank you johanshogun indeed you cleared my concept. – Sahil Aug 21 '15 at 13:39
  • i removed import android.R; and i tried referring to my package but it says """ the import com.example.trial cannot be resolved dont include android.R here;use a fully qualified name for each usage instead"""' – Sahil Aug 21 '15 at 13:48
  • Show your updated code and xml file. – JohanShogun Aug 21 '15 at 14:08
  • i have edited my code. – Sahil Aug 21 '15 at 16:54
1

Why This

R is auto generated resource in android project some times it happens don't exactly know why but have you try build automatically option from project

So for the first issue, if you see at the import section

import android.R;

delete it.

For the second issue, you have to solve your errors at the XML files first, and then when you build your project, your R error will be gone. (You may have to clean your project first).

Clean-Rebuild-Restart 

Please read A comprehensive troubleshooting guide for Android's R cannot be resolved error .I hope it will helps you.

So finally,

package com.example.trial;

import com.example.trial.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class Trial extends Activity {

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

    Button tut1 = (Button) findViewById(R.id.button1);
    tut1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

        startActivity(new Intent(getApplicationContext(), second.class));

        }
    });

}}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198