【问题标题】:Resources$NotFoundException资源$NotFoundException
【发布时间】:2016-06-01 19:11:29
【问题描述】:

所以每当我运行我的应用程序时,它都会立即崩溃并给我以下错误:

No package identifier when getting value for resource number 0x00000000
FATAL EXCEPTION: main
   Process: com.example.wessel.weer, PID: 3095
   android.content.res.Resources$NotFoundException: Resource ID #0x0
       at android.content.res.Resources.getValue(Resources.java:1351)
       at android.content.res.Resources.getDrawable(Resources.java:804)
       at android.content.res.Resources.getDrawable(Resources.java:771)
       at com.example.wessel.weer.MainActivity.serviceSuccess(MainActivity.java:52)
       at com.example.wessel.weer.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:91)
       at com.example.wessel.weer.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:40)
       at android.os.AsyncTask.finish(AsyncTask.java:651)
       at android.os.AsyncTask.-wrap1(AsyncTask.java)
       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

有没有办法手动解决这个问题?我将如何导入库?

编辑:这是我的 MainActivity:

package com.example.wessel.weer;

import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wessel.weer.data.Channel;
import com.example.wessel.weer.data.Item;
import com.example.wessel.weer.service.WeatherServiceCallback;
import com.example.wessel.weer.service.YahooWeatherService;

public class MainActivity extends AppCompatActivity implements WeatherServiceCallback {

private ImageView weatherIconImageView;
private TextView temperatureTextView;
private TextView conditionTextView;
private TextView locationTextView;

private YahooWeatherService service;
private ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    weatherIconImageView = (ImageView)findViewById(R.id.weatherIconImageView);
    temperatureTextView = (TextView)findViewById(R.id.temperatureTextView);
    conditionTextView = (TextView)findViewById(R.id.conditionTextView);
    locationTextView = (TextView)findViewById(R.id.locationTextView);

    service = new YahooWeatherService(this);
    dialog = new ProgressDialog(this);
    dialog.setMessage("Laden...");
    dialog.show();

    service.refreshWeather("Austin, TX");
}

@Override
public void serviceSuccess(Channel channel) {
    dialog.hide();

    Item item = channel.getItem();
    int resourceId = getResources().getIdentifier("drawable/icon_" + item.getCondition().getCode(), null, getPackageName());

    @SuppressWarnings("deprecation")
    Drawable weatherIconDrawable = getResources().getDrawable(resourceId);

    weatherIconImageView.setImageDrawable(weatherIconDrawable);

    temperatureTextView.setText(item.getCondition().getTemperature()+ "\u00B0" +channel.getUnits().getTemperature());
    conditionTextView.setText(item.getCondition().getDescription());
    //conditionTextView.setText(condition.getDescription());
    locationTextView.setText(service.getLocation());
}

@Override
public void serviceFailure(Exception exception) {
    dialog.hide();
    Toast.makeText(this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
}

这里还有activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wessel.weer.MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/weatherIconImageView"
    android:src="@drawable/cna"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/temperature"
    android:id="@+id/temperatureTextView"
    android:layout_below="@+id/weatherIconImageView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/condition"
    android:id="@+id/conditionTextView"
    android:layout_below="@+id/temperatureTextView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/location"
    android:id="@+id/locationTextView"
    android:layout_below="@+id/conditionTextView"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/yahoo_logo"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

希望我现在已经包含了足够多的内容。

【问题讨论】:

  • 没有你需要导入的库。请edit 你的问题在 MainActivity.java 的第 52 行显示你在做什么。您正在加载 ID 为 0 的资源,该资源不存在。
  • 能否提供主要活动
  • 另外,请包含您的布局 XML - 例如您在 setContentView(R.layout.your-layout-name-here) 中引用的那个 - 错误可能指向 XML 中的错误,例如可能有一些拼写错误 - 此类错误将停止生成R.id.
  • 刚刚使用请求的信息编辑了问题。

标签: java android


【解决方案1】:

您没有检查 getIdentifier() 的返回值

int resourceId = getResources().getIdentifier("drawable/icon_" + item.getCondition().getCode(), null, getPackageName());

getIdentifier() 的documentation 表示它

如果没有找到这样的资源,则返回 0。 (0 不是有效的资源 ID。)

要弄清楚为什么没有找到这样的资源,您必须评估和考虑这一点:

"drawable/icon_" + item.getCondition().getCode()

不用说,这提供了在运行时评估不在 apk 中打包的固定资源列表中的东西的机会。

【讨论】:

  • 但是..我在 Resourses.java 中也有很多错误,这不会增加问题吗? imgur.com/ct6Pbju
  • 我只是再次检查,结果发现我放置在drawable中的图像实际上不包含icon_前缀。我已更改此设置,但仍然出现错误。我应该更新 OP 吗?
【解决方案2】:

虽然您可以使用drawable/icon_ 作为参数,但null 参数介于RR.xxx.resource 中的resource 之间。因此,您可以改为使用

String iconName =  "icon_" + item.getCondition().getCode();
int resId = getResources().getIdentifier(icon, "drawable", getPackageName());

至于为什么会出现错误,该方法记录为

如果没有找到这样的资源,则返回 0。 (0 不是有效的资源 ID。)

所以调试并找出item.getCondition().getCode() 返回的内容以及为什么drawable 的文件夹中没有icon_xyz 具有该编号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多