java - 资源$NotFoundException

标签 java android

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

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)

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

编辑:这是我的主要 Activity :

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>

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

最佳答案

您未能检查 getIdentifier() 的返回值

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

documentation getIdentifier() 说它

Returns 0 if no such resource was found. (0 is not a valid resource ID.)

要弄清楚为什么找不到此类资源,您必须评估并考虑这一点:

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

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

关于java - 资源$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576942/

相关文章:

java - 如何使用 Nailgun Server 启动 Groovy 脚本(或 Java 类)

java - 在 ajax spring mvc 中返回 ModelAndView

java - 如何将匿名 JavaScript 对象从 Java 传递到 GWT 中的 JavaScript?

android - 如何在 Android 应用程序中传递引用?

android - ViewPager + PagerSlidingTabStrip : Navigating from a tab immediately without displaying contents while scrolling

Java 包装器 : overriding a method called in the super constructor

java - 如果墙纸是从另一个应用程序设置的,如何停止服务

java - 使用 gmail api 和 android 客户端获取完整的电子邮件正文内容

android - 在 Android 中以编程方式在当前相对布局下方添加相对布局

java - 如何使用 Java 解析 php 消息 - HTTP 格式不正确的问题