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


    }
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
SAK
  • 51
  • 1
  • 8
  • If you have a second question, then ask a second question separately. But check more carefully for duplicates first. – OrangeDog May 31 '16 at 22:40

1 Answers1

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.

Lal
  • 14,726
  • 4
  • 45
  • 70
  • Check my updated answer @SAK – Lal May 31 '16 at 17:43
  • 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 – SAK May 31 '16 at 17:56
  • not so sure that i'l be able to debug it..anyway just post it..may be some others could help you.. – Lal May 31 '16 at 17:57
  • 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 – SAK May 31 '16 at 18:22
  • please share the code at which this error is raised.. – Lal May 31 '16 at 18:24
  • Edit the question with the xml code... – Lal May 31 '16 at 18:29
  • I just put in the styles.xml code – SAK May 31 '16 at 18:30
  • what is `...` in `...` ? is there something else? – Lal May 31 '16 at 18:31
  • there isnt anything else – SAK May 31 '16 at 18:32
  • there lies the problem...replace that `...` with some colour codes..remember that colour codes should be given in hexadecimal values..eg: `#ffffff` for white. – Lal May 31 '16 at 18:33
  • I had this originally @color/colorPrimary @color/colorPrimaryDark @color/colorAccent but it gave me an error so I changed it – SAK May 31 '16 at 18:33
  • ok..do you have an entry named `colorPrimary` in your color.xml file..If there is no such entry, then it will be an error.. – Lal May 31 '16 at 18:34
  • I entered color hexas and I think it works now. Thankyou – SAK May 31 '16 at 18:36
  • Great.. :) glad that it helped you.. – Lal May 31 '16 at 18:36