3

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();


    }
1
  • If you have a second question, then ask a second question separately. But check more carefully for duplicates first. Commented May 31, 2016 at 22:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

14 Comments

Check my updated answer @SAK
That makes sense I recently edited the color.xml file so maybe that's it? I can post the code from there as well as the android manifest xml file code
not so sure that i'l be able to debug it..anyway just post it..may be some others could help you..
actually my styles.xml file is giving me the error:string types not allowed (at 'colorAccent' with value'...'). It wont let me post the code as it is too long
please share the code at which this error is raised..
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.