Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 842
Opal Avatar asked Dec 01 '25 03:12

Opal


1 Answers

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

like image 149
RTF Avatar answered Dec 02 '25 18:12

RTF