ErinSKabbash
ErinSKabbash

Reputation: 389

Not finding ImageView?

I am trying to do something very basic and I cannot figure out why it will not find the ImageView from the Java code. When I type R.id.scanButton I get an error saying that resource does not exist.. but as you can see in my XML it has been created. What am I missing here?

MainActivity.java

package com.erinkabbash.coolmeter;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

        ImageView button = (ImageView) findViewById(R.id.scanButton); // ImageView not Found
    }


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

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

activity_main.xml

<RelativeLayout 
    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"
    tools:context="com.erinkabbash.coolmeter.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/resultView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/scanButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/scanButton" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Views: 281

Answers (3)

Andrew Quebe
Andrew Quebe

Reputation: 2293

resultView and scanButton should be lower-cased to resultview and scanbutton. The R.java file is very picky about upper-case letters and special characters anywhere in your res folder.

Upvotes: 0

Lavekush
Lavekush

Reputation: 6166

import R.java

com.erinkabbash.coolmeter.R;   //Your.base.pkg.com.R   /* You base package .R*/

And clean your project. it might be work,

If you working with your base package domain ,then there is no need of importing R.java. just Clean your project or workspace. . It will work.

Only use lowercase and underscore  for res/, drawables/ and others res fies

Upvotes: 1

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

There is nothing wrong in your code, You just need to Clean and Build your project to register your id that was added.

Upvotes: 1

Related Questions