简体   繁体   English

资源$ NotFoundException

[英]Resources$NotFoundException

So whenever i run my app, it'll instantly crash and give me the following error: 因此,每当我运行我的应用程序时,它都会立即崩溃并给我以下错误:

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)

Is there a way to manually fix this? 有没有办法手动解决此问题? How would i import the library? 我将如何导入库?

EDIT: Here's my MainActivity: 编辑:这是我的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();
}
}

Also here's the activity_main.xml: 这也是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>

Hope i've included enough now. 希望我现在已经足够了。

You are failing to check the return value of getIdentifier() 您无法检查getIdentifier()的返回值

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

The documentation of getIdentifier() says that it getIdentifier()的文档说它

Returns 0 if no such resource was found. 如果找不到这样的资源,则返回0。 (0 is not a valid resource ID.) (0不是有效的资源ID。)

To figure out why no such resource is being found, you would have to evaluate and contemplate this: 为了弄清楚为什么找不到这样的资源,您必须对此进行评估和考虑:

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

which needless to say offers opportunities to evaluate at runtime to something that is not in the fixed list of resources packed in the apk. 不用说,这提供了在运行时评估apk中未包含的固定资源列表中某些内容的机会。

While you can use drawable/icon_ as the parameter, that null argument is what goes between the R and the resource in R.xxx.resource . 虽然可以将drawable/icon_用作参数,但是null参数是RR.xxx.resourceresource之间的R.xxx.resource Therefore, you could instead have 因此,您可以改为

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

As for why you are getting an error, that method is documented as 至于为什么出现错误,该方法记录为

Returns 0 if no such resource was found. 如果找不到这样的资源,则返回0。 (0 is not a valid resource ID.) (0不是有效的资源ID。)

So debug and figure out what item.getCondition().getCode() returns and why there is no icon_xyz in the drawable's folder with that number. 因此,调试并找出item.getCondition().getCode()什么,以及为什么在可绘制文件夹中没有该编号的icon_xyz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM