簡體   English   中英

為什么在Android Studio中出現此錯誤“原始類型的預期資源”?

[英]Why am I getting this error “Expected resource of type raw” in Android Studio?

我試圖使用按鈕設置默認牆紙,但是由於某種原因,當我在OnCreate方法中設置InputStream時,出現此錯誤“ raw類型的預期資源”。 我引用了drawable文件夾,並使用了drawable文件夾中的icon.png圖像。 我正在遵循NewBoston系列中的教程,它對於Travis似乎工作正常,但是由於某些原因,我的作品在Android Studio中不起作用。 可能是什么錯誤? 謝謝

Camera.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);

        }
    }
}

圖片:

圖片

發生此錯誤是因為Android Studio預期資源文件的類型為raw。

解決方案1:

在“ res”文件夾中創建一個名為“ raw”的新文件夾,並將圖標放在此處。 原始文件夾應包含您應用程序的所有媒體文件。

然后更換

InputStream is = getResources().openRawResource(R.drawable.icon);

InputStream is = getResources().openRawResource(R.raw.icon);

解決方案2:

另一個解決方案是這樣做。 這不需要您創建原始文件夾:

InputStream is = getResources().openRawResource(+ R.drawable.icon);

更換

InputStream is = getResources().openRawResource(R.drawable.icon);

InputStream is = getResources().openRawResource(+ R.drawable.icon);

我們來看一下openRawResource文檔

打開數據流以讀取原始資源。 它只能與值是資產文件名稱的資源一起使用-也就是說,它可以用於打開可繪制,聲音和原始資源 它將在字符串和顏色資源上失敗。

因此,解決方案-保持原樣 ,即

InputStream is = getResources().openRawResource(R.drawable.icon);

Android Studio中的檢查是錯誤的。 你可以加

@SuppressWarnings("ResourceType")

隱藏錯誤。

注意

使用+ R.drawable.icon只是愚弄了檢查,因為它無法再確定它是哪種資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM