gpt4 book ai didi

java - 资源$NotFoundException

转载 作者:行者123 更新时间:2023-12-01 09:51:22 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com