3

I was trying to run my project in my Genymotion emulator, but none of the new updated buttons that I added were showing when I ran my Android application. So, I made a copy of my project to run a "clean" on it because I've had problems with my R.java file not being generated after cleaning it, and I was right as the R.java file didn't generate in the copied project. I'm almost sure it's a problem in my XML file, but my XML file shows no errors.

I also had the same error before, which I posted about here: R cannot be resolved to a variable?

The R file is not being generated so all my calls in MainActivity like mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.crayon); are stating "R cannot be resolved to a variable" which I know points to a problem in my XML file.

Here is my activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:gravity="center"
   tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|fill_horizontal|center"
    android:orientation="horizontal" >

    <RadioGroup
        android:id="@+id/greyorcolor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/grey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/reyscale" />

        <RadioButton
            android:id="@+id/color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/color" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/smallorlarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/large" />

        <RadioButton
            android:id="@+id/small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/small" />
    </RadioGroup>
</LinearLayout>

<edu.berkeley.cs160.opalkale.prog2.DrawingView
    android:id="@+id/drawing"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

My strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">Drawing</string>
   <string name="greyscale">Greyscale</string>
   <string name="color">Color</string>
   <string name="small">Small Brush</string>
   <string name="large">Large Brush</string>
   <string name="hello_world">Hello world!</string> </resources>

My MainActivity.java file:

package edu.berkeley.cs160.opalkale.prog2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class MainActivity extends Activity {

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

    Bitmap munBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.crayon);
    Bitmap mBitmap = munBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mBitmap);

    DrawingView drawingView = (DrawingView) findViewById(R.id.drawing);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Problems window: enter image description here enter code here

Community
  • 1
  • 1
Opal
  • 471
  • 1
  • 7
  • 20

5 Answers5

2

I'm not exactly clear about this, but are you using eclipse? If you are, I've had similar issues where R.java was not being generated, but eclipse was not flagging any errors. In all cases, there was actually errors in the XML layout files, but I to detect them I had to open the "Problems" view to find out what they are. If you don't have it open already, you could try that: Window -> Show View -> Problems

RTF
  • 6,214
  • 12
  • 64
  • 132
  • Yes I am, see here for my previous post, I added a picture of my "Problems" http://stackoverflow.com/questions/21768370/r-cannot-be-resolved-to-a-variable – Opal Feb 14 '14 at 05:18
  • I also just posted what I see in my problems log above! – Opal Feb 14 '14 at 05:25
0

This happens since eclipse is not able to build the project. Try restarting eclipse and see if R file gets created. If not clean again now and see if it works.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
0

You have one special character in radiobutton's name @

    <RadioButton
        android:id="@+id/grey"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@Greyscale" />                  // Here 

You need to remove that @

user3301551
  • 350
  • 1
  • 14
  • OP has updated his question. He has fixed this "typo" but still the problem is not solved. – Andrew T. Feb 14 '14 at 04:47
  • @Mehul That is an Invalid Edit. Don't do this again. – user3301551 Feb 14 '14 at 04:47
  • @MehulJoisar You must not add any content to existing post while editing. – user3301551 Feb 14 '14 at 04:49
  • @user3301551: I tried to improve your answer so that you can learn to answer.anyways,be happy with downvotes.I dont mind :-) – Mehul Joisar Feb 14 '14 at 04:52
  • @MehulJoisar I appreciate your work but still it is invalid edit as per rule. And I dont care about downvotes if my answer is wrong. – user3301551 Feb 14 '14 at 04:55
  • @user3301551: No offence but kindly look at [this](http://meta.stackoverflow.com/help/privileges/edit) . and you will find out the rule for 'Moderation Privilege' to make the posts better :-) – Mehul Joisar Feb 14 '14 at 05:02
  • @MehulJoisar Opps You right. `to correct minor mistakes or add addendums / updates as the post ages to add related resources or hyperlinks` is legal. I am sorry. – user3301551 Feb 14 '14 at 05:04
0

That's happen many times to me, that's why I'm using Android studio,

Let's come to main Answer :)

Firstly, save your project Clean again, if still not generating your R.java file, then Restart again. may hope that would work :)

Here is my question when i posted it. :)

Suddently, "R.java" was deleted from my "CheckBox" program?

Community
  • 1
  • 1
Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
  • I've tried to restart, clean, etc many times and nothing is changing. It's definitely a problem in my XML file – Opal Feb 14 '14 at 04:49
0

there are two possibilities of the errror:

1: you forgot to add drawable named "crayon" in drawable folder.

2: package error: you just need to check the import statements. it should look like

import com.test.example.R;

where your package name = com.test.example

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • crayon is a jpg in drawable (just double checked. where should I look for the import statement to see if it's there? In my custom views class or in my main activity? – Opal Feb 14 '14 at 04:50
  • @Opal: in your main activity – Mehul Joisar Feb 14 '14 at 04:51
  • I updated to show my MainActivity.java file. It is not a problem of importing the R.java file if it is not being generated in the first place – Opal Feb 14 '14 at 05:02