eclipse 为什么我在 Android Studio 中收到此错误“预期的原始类型资源”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32038476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting this error "Expected resource of type raw" in Android Studio?
提问by Missy Zewdie
I was trying to set the default wallpaper by using a button but for some reason when i set the InputStream in the OnCreate Method, i get this error "expected resource of type raw". I am referencing the drawable folder and using the icon.png image which is in the drawable folder. I was following the tutorials in the NewBoston Series and it seems to work fine for Travis but for some reason mine doesnt work in Android Studio. What could be the error? Thanks
我试图通过使用按钮设置默认墙纸,但由于某种原因,当我在 OnCreate 方法中设置 InputStream 时,我收到此错误“预期的原始类型资源”。我正在引用 drawable 文件夹并使用 drawable 文件夹中的 icon.png 图像。我正在关注 NewBoston 系列中的教程,它似乎对 Travis 工作正常,但由于某种原因,我的在 Android Studio 中不起作用。可能是什么错误?谢谢
Camera.java:
相机.java:
package com.example.user.cameraapplication;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by user on 16-08-2015.
*/
public class Camera extends Activity implements View.OnClickListener{
ImageView iv;
Button b1,b2;
ImageButton img;
Intent i;
final static int cameractivity = 0;
Bitmap b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
inititalize();
InputStream is = getResources().openRawResource(R.drawable.icon);
b = BitmapFactory.decodeStream(is);
}
private void inititalize() {
iv = (ImageView)findViewById(R.id.iView1);
img = (ImageButton)findViewById(R.id.imgbtn);
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
try {
getApplicationContext().setWallpaper(b);
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.imgbtn:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameractivity);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras = data.getExtras();
b = (Bitmap)extras.get("data");
iv.setImageBitmap(b);
}
}
}
Image:
图片:
回答by Hussein El Feky
The error occurred because Android Studio expected a resource file of type raw.
发生错误是因为 Android Studio 需要原始类型的资源文件。
Solution 1:
解决方案1:
Create a new folder in your "res" folder called "raw", and put your icon there. The raw folder should contain all your media files of your app.
在您的“res”文件夹中创建一个名为“raw”的新文件夹,并将您的图标放在那里。原始文件夹应包含应用程序的所有媒体文件。
Then replace
然后更换
InputStream is = getResources().openRawResource(R.drawable.icon);
with
和
InputStream is = getResources().openRawResource(R.raw.icon);
Solution 2:
解决方案2:
Another solution is to do it like this. This doesn't require you to create a raw folder:
另一种解决方案是这样做。这不需要您创建原始文件夹:
InputStream is = getResources().openRawResource(+ R.drawable.icon);
回答by Pavlo Zin
Replace
代替
InputStream is = getResources().openRawResource(R.drawable.icon);
With
和
InputStream is = getResources().openRawResource(+ R.drawable.icon);
回答by Leo Leontev
Let's look at documentation for openRawResource
让我们看一下文档 openRawResource
Open a data stream for reading a raw resource. This can only be used with resources whose value is the name of an asset files -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color resources.
打开数据流以读取原始资源。这只能用于其值为资产文件名称的资源——也就是说,它可以用于打开可绘制、声音和原始资源;它将在字符串和颜色资源上失败。
So the solution - leave it as it is, i. e.
所以解决方案 - 保持原样,即
InputStream is = getResources().openRawResource(R.drawable.icon);
The inspection in Android Studio is just wrong. You can add
Android Studio 中的检查是错误的。你可以加
@SuppressWarnings("ResourceType")
to hide the error.
隐藏错误。
Note
笔记
Using + R.drawable.icon
just fools the inspection, because it can no longer determine which kind of resource it is.
使用+ R.drawable.icon
只是愚弄检查,因为它无法再确定它是哪种资源。