SAK SAK - 6 months ago 88
Java Question

Java:package R does not exist

I am creating a simple facts app and have followed a tutorial thoroughly but am getting the error Cannot resolve symbol R in my Main Activity. I tried importing mypackagename.R but that did not work either

My package name is:
package com.example.saarikakumar.myapplication;

My main activity code is as follows:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.saarikakumar.myapplication.android.R;

public class MainActivity extends AppCompatActivity {

TextView factBox;
LinearLayout bg;
Facts factHolder = new Facts();
Backgrounds backs = new Backgrounds();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factBox = (TextView) findViewById(R.id.factTextbox);
bg = (LinearLayout) findViewById(R.id.background);
bg.setBackgroundColor(getResources().getColor(backs.getbackground()));
bg.setOnTouchListener(new OnSwipeTouchListener(this) {

public void onSwipeTop() {

}

public void onSwipeRight() {

prev();
}
public void onSwipeLeft() {

next();
}
public void onSwipeBottom() {

}

public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});

}

private void prev() {
factBox.setText(factHolder.prevFact());
bg.setBackgroundColor(getResources().getColor(backs.getbackground()));

}

private void next() {
factBox.setText(factHolder.nextFact());
bg.setBackgroundColor(getResources().getColor(backs.getbackground()));



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


public void genFact(View view) {
next();


}

Lal Lal
Answer

Remove the below given import statement from your code

import com.example.saarikakumar.myapplication.android.R;

You don't have to have import any R.java files.

If you have any error saying that Cannot resolve symbol R, then it means that the R.java file is not generated because you have got some error in your xml files. So you should check your xml files for errors and fix it. R.java files will be generated automatically.

First of all, just see if "clean and rebuild" helps you solve the problem.

Comments