【发布时间】:2016-06-01 19:11:29
【问题描述】:
所以每当我运行我的应用程序时,它都会立即崩溃并给我以下错误:
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>
希望我现在已经包含了足够多的内容。
【问题讨论】:
-
没有你需要导入的库。请edit 你的问题在 MainActivity.java 的第 52 行显示你在做什么。您正在加载 ID 为 0 的资源,该资源不存在。
-
能否提供主要活动
-
另外,请包含您的布局 XML - 例如您在
setContentView(R.layout.your-layout-name-here)中引用的那个 - 错误可能指向 XML 中的错误,例如可能有一些拼写错误 - 此类错误将停止生成R.id. -
刚刚使用请求的信息编辑了问题。