Opal
Opal

Reputation: 491

Cleaned project, my R.java file won't generate because of error in XML file?

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

Upvotes: 3

Views: 2811

Answers (5)

RTF
RTF

Reputation: 6494

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

Upvotes: 2

user3301551
user3301551

Reputation: 350

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 @

Upvotes: 0

Mehul Joisar
Mehul Joisar

Reputation: 15358

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

Upvotes: 0

Rajendra arora
Rajendra arora

Reputation: 2224

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?

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

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.

Upvotes: 0

Related Questions