找不到资源异常

时间:2016-06-05 19:09:16

标签: java android android-studio resources

因此,在加载我的应用时出现以下崩溃:

--------- 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$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)

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

大多数(如果不是所有)resources.java中的错误都是:

Cannot resolve symbol 'symbol name here'

例如:

Cannot resolve symbol 'DrawableRes'

at

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$1.onPostExecute(YahooWeatherService.java:87)
                  at com.example.wessel.weerapplicatie.service.YahooWeatherService$1.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)

1 个答案:

答案 0 :(得分:0)

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

它说:

  

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

很明显,你在resourceId中得到的不是有效的resourceId。 记录您从服务中获得的内容,看看您是否拥有可绘制的资源。

你也许我缺少@ here:

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

您也可以使用

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