LordSlickRick
LordSlickRick

Reputation: 49

cannot match r.id in xml file to main activity

First time on stack overflow so i apologize if i did this wrong.

I dont see why the id drawing specified cannot be referenced in MainActivity, it simply gives an error under the word drawing and paint_colors and paint_pressed. It suggests creating a fieldin type id.

Here is a partial of the code in the xml

android:id="@+id/drawing"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >
    <!-- Top Row -->
<LinearLayout
    android:id="@+id/paint_colors"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageButton
    android:layout_width="@dimen/large_brush"
    android:layout_height="@dimen/large_brush"
    android:layout_margin="2dp"
    android:background="#FF660000"
    android:contentDescription="@string/paint"
    android:onClick="paintClicked"
    android:src="@drawable/paint"
    android:tag="#FF660000" />

Here is the code in the main activity

   package com.example.drawingapprick;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {
    private DrawingPlace drawPlace;
    private ImageButton currPaint;
    public void paintClicked(View view){
        //use chosen color
        if(view!=currPaint){
            //update color
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();
            drawPlace.setColor(color);
            }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawPlace = (DrawingPlace)findViewById(R.id.drawing);
        LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
        currPaint = (ImageButton)paintLayout.getChildAt(0);
        currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
    }

    @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;
    }

}

Upvotes: 2

Views: 260

Answers (3)

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

Remove android.R from your imports.

Upvotes: 0

kai
kai

Reputation: 534

Check your imports. Maybe you import android.R instead of your R class

Upvotes: 2

Apoorv
Apoorv

Reputation: 13520

It seems you have probably imported android.R rather than your.package.name.R.

Upvotes: 2

Related Questions