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'
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With