Android Gradle: 找不到符号变量
Android Gradle: can't find symbol variable
我在使用 gradle 构建时遇到错误,即 error: cannot find symbol variable [image_name]
。我正在使用 ContextCompat.getDrawable(getActivity(), R.drawable.[image_name])
我一直在谷歌搜索,直到我发现这种方法可以在不使用已弃用的方法或将我的 min sdk 设置为 21 的情况下获取可绘制对象。但是现在,gradle 说它不能找到一个符号变量。我的图像在可绘制文件夹中。
有什么需要的,欢迎留言。
编辑:
结果:
可绘制(图片在此处)
drawable-hdpi
...
我的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'
}
您已导入android.R
,其中仅包含核心框架资源。您需要将其更改为 com.example.package.R
,其中 com.example.package
是您的应用程序资源包名称。
编辑(MashedPotatoes):我在没有正确转换的情况下将 JPG 文件强制为 PNG,这就是导致错误的原因。但最初的错误是由于导入 android.R
而不是 com.example.package.R
引起的,因此将其标记为正确。
对我而言,该错误与导入无关,只是处理了具有 .PNG 文件扩展名的 .JPG。重命名文件扩展名解决了问题。
我在使用 gradle 构建时遇到错误,即 error: cannot find symbol variable [image_name]
。我正在使用 ContextCompat.getDrawable(getActivity(), R.drawable.[image_name])
我一直在谷歌搜索,直到我发现这种方法可以在不使用已弃用的方法或将我的 min sdk 设置为 21 的情况下获取可绘制对象。但是现在,gradle 说它不能找到一个符号变量。我的图像在可绘制文件夹中。
有什么需要的,欢迎留言。
编辑:
结果:
可绘制(图片在此处)
drawable-hdpi
...
我的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'
}
您已导入android.R
,其中仅包含核心框架资源。您需要将其更改为 com.example.package.R
,其中 com.example.package
是您的应用程序资源包名称。
编辑(MashedPotatoes):我在没有正确转换的情况下将 JPG 文件强制为 PNG,这就是导致错误的原因。但最初的错误是由于导入 android.R
而不是 com.example.package.R
引起的,因此将其标记为正确。
对我而言,该错误与导入无关,只是处理了具有 .PNG 文件扩展名的 .JPG。重命名文件扩展名解决了问题。