资源$ NotFoundException

时间:2016-06-01 19:11:29

标签: 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)

有没有办法手动修复此问题?我如何导入图书馆?

编辑:这是我的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>

希望我现在已经包括在内了。

2 个答案:

答案 0 :(得分:1)

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

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

getIdentifier()的documentation表示它

  

如果未找到此类资源,则返回0。 (0不是有效的资源ID。)

要弄清楚为什么没有找到这样的资源,你必须评估并考虑这个:

"drawable/icon_" + item.getCondition().getCode()
不用说,

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

答案 1 :(得分:0)

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

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

至于您收到错误的原因,该方法记录为

  

如果未找到此类资源,则返回0。 (0不是有效的资源ID。)

因此,调试并找出item.getCondition().getCode()返回的内容以及为什么在drawable的文件夹中没有icon_xyz的数字。