找不到后台资源

时间:2013-01-15 01:42:54

标签: android relativelayout

我从谷歌收到此错误日志,我不知道是什么导致它。此资源位于文件夹中,表示不是。我自己从未遇到过这个错误。这是错误:

android.content.res.Resources$NotFoundException: File res/drawable-mdpi/notebook_ul.jpg from drawable resource ID #0x7f020090
at android.content.res.Resources.loadDrawable(Resources.java:1714)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.view.View.setBackgroundResource(View.java:7391)
at com.bfreq.dice.fragments.SettingsFrag.onCreateView(SettingsFrag.java:76)
..............................
Caused by: java.lang.NullPointerException.........

这是我在我的片段中调用资源

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.settings, container, false);

        // Initializing Background
        rLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout_Settings);
        switch (background) {
        case 0:
            rLayout.setBackgroundResource(R.drawable.clay_ul);
            break;
        case 1:
            rLayout.setBackgroundResource(R.drawable.parchment_ul);
            break;
        case 2:
            rLayout.setBackgroundResource(R.drawable.brownpaper_ul);
            break;
        case 3:
            rLayout.setBackgroundResource(R.drawable.notebook_ul);
            break;
        case 4:
            rLayout.setBackgroundColor(Color.WHITE);
            break;
        }
            ........
            return v;
        }

我也是从方法中调用它。现在我知道我不会直接从一个片段到另一个片段进行对话,但它一直没有问题,没有时间重写所有内容。

public void setBackground() {

    switch (background) {
    case 0:
        SettingsFrag.rLayout.setBackgroundResource(R.drawable.clay_ul);
        DiceFrag.rLayout.setBackgroundResource(R.drawable.clay_uc);
        DiceLogFrag.rLayout.setBackgroundResource(R.drawable.clay_ur);
        break;
    case 1:
        SettingsFrag.rLayout.setBackgroundResource(R.drawable.parchment_ul);
        DiceFrag.rLayout.setBackgroundResource(R.drawable.parchment_uc);
        DiceLogFrag.rLayout.setBackgroundResource(R.drawable.parchment_ur);
        break;
    case 2:
        SettingsFrag.rLayout.setBackgroundResource(R.drawable.brownpaper_ul);
        DiceFrag.rLayout.setBackgroundResource(R.drawable.brownpaper_uc);
        DiceLogFrag.rLayout.setBackgroundResource(R.drawable.brownpaper_ur);
        break;
    case 3:
        SettingsFrag.rLayout.setBackgroundResource(R.drawable.notebook_ul);
        DiceFrag.rLayout.setBackgroundResource(R.drawable.notebook_uc);
        DiceLogFrag.rLayout.setBackgroundResource(R.drawable.notebook_ur);
        break;
    case 4:
        SettingsFrag.rLayout.setBackgroundColor(Color.WHITE);
        DiceFrag.rLayout.setBackgroundColor(Color.WHITE);
        DiceLogFrag.rLayout.setBackgroundColor(Color.WHITE);
        break;
    }
}

每当我调用该方法时,我都会确保它不会返回空指针。任何想法都会很棒。如果有人想看,我还可以添加更多代码。谢谢!

1 个答案:

答案 0 :(得分:2)

当我为特定dpi创建一个资产(即hdpi,xhdpi ...)时,我得到此信息,请确保您的drawable在drawable/drawable-hdpi/,{{1}中可用您可能在一个drawable-mdpi/目录中创建了一个drawable,但不是当前仿真器/设备所从的目录。