找不到资源异常

Resources not found exception

所以我在加载我的应用程序时遇到以下崩溃:

--------- beginning of crash
06-05 18:56:56.457 2545-2545/com.example.wessel.weer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.example.wessel.weer, PID: 2545
                                                                   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.onPostExecute(YahooWeatherService.java:91)
                                                                       at com.example.wessel.weer.service.YahooWeatherService.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)

我认为问题与我的 Resources.java 有关,其中总共有 219 个错误和 31 个警告。

resources.java 中的大部分错误是:

Cannot resolve symbol 'symbol name here'

例如:

Cannot resolve symbol 'DrawableRes'

public Drawable getDrawable(@DrawableRes int id) throws NotFoundException {

我已经尝试创建一个新项目并复制旧项目类,但这是同样的问题。

要求的主要活动代码:

package com.example.wessel.weerapplicatie;

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

import com.example.wessel.weer.R;
import com.example.wessel.weerapplicatie.data.Channel;
import com.example.wessel.weerapplicatie.data.Item;
import com.example.wessel.weerapplicatie.service.WeatherServiceCallback;
import com.example.wessel.weerapplicatie.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();
}
}

使用建议的解决方案后出错:

                 --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.wessel.weerapplicatie, PID: 2446
              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.weerapplicatie.MainActivity.serviceSuccess(MainActivity.java:53)
                  at com.example.wessel.weerapplicatie.service.YahooWeatherService.onPostExecute(YahooWeatherService.java:87)
                  at com.example.wessel.weerapplicatie.service.YahooWeatherService.onPostExecute(YahooWeatherService.java:36)
                  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)

https://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)

它说:

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

很明显,您在 resourceId 中得到的不是有效的 resourceId。 记录您从服务中获得的内容,并查看您在可绘制对象中是否有确切的资源。

另外你可能我这里缺少@:

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

你也可以使用

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