mashedpotats
mashedpotats

Reputation: 324

Android Gradle: can't find symbol variable

I have a error when building with gradle that says, error: cannot find symbol variable [image_name]. I'm using ContextCompat.getDrawable(getActivity(), R.drawable.[image_name]) I have been googling all over until I found this method to get a drawable without using a deprecated method or setting my min sdk to 21. But now, gradle says it can't find a symbol variable. I have my image in the drawable folder. If you need anything, comment please.

EDIT:

res:
drawable (images under here)
drawable-hdpi
...

My class:

import [package_name].R;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;

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

public HomeActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String[] titleData = {"[TEXT]", "[TEXT]", "[TEXT]"
    };

    ArrayList<String> titles = new ArrayList<String>(Arrays.asList(titleData));

    Drawable[] imageData = {
            ContextCompat.getDrawable(getActivity(), R.drawable.seminar_cross_th),
            ContextCompat.getDrawable(getActivity(), R.drawable.media_th),
            ContextCompat.getDrawable(getActivity(), R.drawable.praise_th)
    };

    ArrayList<Drawable> images = new ArrayList<Drawable>(Arrays.asList(imageData));

    HomeAdapter homeListAdapter = new HomeAdapter(getActivity(), titles, images);


    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.home_list);
    listView.setAdapter(homeListAdapter);

    return rootView;
}
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exampleApp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.exampleTheme" >
    <activity
        android:name=".HomeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.example.exampleApp"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.+'
compile 'com.github.navasmdc:MaterialDesign:1.5@aar'
compile 'com.balysv:material-ripple:1.0.2'
}

Upvotes: 5

Views: 7016

Answers (2)

AlexD
AlexD

Reputation: 833

For me the error had nothing to do with imports and simply was dealing with a .JPG that had a .PNG file extension. Renaming the file extension fixed the problem.

Upvotes: 0

Kane O&#39;Riley
Kane O&#39;Riley

Reputation: 2528

You have imported android.R, which only includes core framework resources. You need to change this to com.example.package.R, where com.example.package is your applications resource package name.

EDIT (MashedPotatoes): I forced a JPG file to be PNG without proper conversion, that's what caused the errors. But the original error was caused by importing android.R, not com.example.package.R, so marking this as correct.

Upvotes: 6

Related Questions