您好我是编程新手,我在Log Cat中遇到此错误。 Log Cat显示'FATAL EXCEPTION Main'并且我的应用程序意外停止。我试图在几天内处理它。应用程序应显示电池信息
05-02 22:01:11.836: I/Process(521): Sending signal. PID: 521 SIG: 9
05-02 22:18:54.743: D/dalvikvm(550): GC_EXTERNAL_ALLOC freed 1238 objects / 82472 bytes in 181ms
05-02 22:18:55.654: W/ResourceType(550): No package identifier when getting value for resource number 0x00000032
05-02 22:18:55.664: D/AndroidRuntime(550): Shutting down VM
05-02 22:18:55.664: W/dalvikvm(550): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-02 22:18:55.744: E/AndroidRuntime(550): FATAL EXCEPTION: main
05-02 22:18:55.744: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to resume activity {vader.batterydroid/vader.batterydroid.PageInfo}: android.content.res.Resources$NotFoundException: String resource ID #0x32
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.os.Looper.loop(Looper.java:123)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-02 22:18:55.744: E/AndroidRuntime(550): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 22:18:55.744: E/AndroidRuntime(550): at java.lang.reflect.Method.invoke(Method.java:521)
05-02 22:18:55.744: E/AndroidRuntime(550): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-02 22:18:55.744: E/AndroidRuntime(550): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-02 22:18:55.744: E/AndroidRuntime(550): at dalvik.system.NativeStart.main(Native Method)
05-02 22:18:55.744: E/AndroidRuntime(550): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x32
05-02 22:18:55.744: E/AndroidRuntime(550): at android.content.res.Resources.getText(Resources.java:201)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.widget.TextView.setText(TextView.java:2817)
05-02 22:18:55.744: E/AndroidRuntime(550): at vader.batterydroid.PageInfo.updateViews(PageInfo.java:171)
05-02 22:18:55.744: E/AndroidRuntime(550): at vader.batterydroid.PageInfo.updateInfo(PageInfo.java:77)
05-02 22:18:55.744: E/AndroidRuntime(550): at vader.batterydroid.PageInfo.access$0(PageInfo.java:74)
05-02 22:18:55.744: E/AndroidRuntime(550): at vader.batterydroid.PageInfo$BatteryInfoReceiver.onReceive(PageInfo.java:255)
05-02 22:18:55.744: E/AndroidRuntime(550): at vader.batterydroid.PageInfo.onResume(PageInfo.java:242)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.Activity.performResume(Activity.java:3823)
05-02 22:18:55.744: E/AndroidRuntime(550): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
05-02 22:18:55.744: E/AndroidRuntime(550): ... 12 more
这是我的代码:
package vader.batterydroid;
import android.annotation.TargetApi;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Date;
public class PageInfo extends Activity{
private static final boolean DEBUG = false;
private static final String NOT_AVAIABLE = "----";
private static final String TAG = "PageInfo";
private static final int TEMPERATURE_NOT_AVAILABLE = -9999;
BatteryInfoReceiver mBatteryIR;
java.text.DateFormat mDF;
IconDrawer mDrawer;
private int mHealth;
private int mLevel;
private SharedPreferences mPM;
private int mPlugged;
private boolean mPresent;
private int mRemainingCapacity;
private int mScale;
private int mStatus;
private String mTechnology;
private int mTemperature;
private long mUpdateTime;
private int mVoltage;
private Button mbtSettings;
private ImageView mivGraph;
private TextView mtvCap;
private TextView mtvHealth;
private TextView mtvLastUpdate;
private TextView mtvPlugged;
private TextView mtvPresent;
private TextView mtvStatus;
private TextView mtvTechnology;
private TextView mtvTemperature;
private TextView mtvVoltage;
private void parseIntent(Intent paramIntent){
this.mUpdateTime = System.currentTimeMillis();
this.mPresent = paramIntent.getBooleanExtra("present", false);
this.mTechnology = paramIntent.getStringExtra("technology");
this.mHealth = paramIntent.getIntExtra("health", 0);
this.mStatus = paramIntent.getIntExtra("status", 0);
this.mPlugged = paramIntent.getIntExtra("plugged", 0);
this.mLevel = paramIntent.getIntExtra("level", 0);
this.mScale = paramIntent.getIntExtra("scale", 0);
this.mVoltage = paramIntent.getIntExtra("voltage", 0);
this.mTemperature = paramIntent.getIntExtra("temperature", -9999);
if (this.mScale > 0){
this.mRemainingCapacity = (100 * this.mLevel / this.mScale);
return;
}
this.mRemainingCapacity = this.mLevel;
}
private void updateInfo(Context paramContext, Intent paramIntent)
{
parseIntent(paramIntent);
updateViews();
updateWidgetDemo(paramIntent);
}
private void updateViews()
{
Object localObject1;
Object localObject2;
Object localObject3;
if (this.mPresent)
{
this.mtvPresent.setText(getResources().getText(R.string.info_text_battery_present_yes));
if (this.mTechnology == null)
this.mtvTechnology.setText(this.mTechnology);
switch (this.mHealth)
{
default:
localObject1 = "----";
this.mtvHealth.setText((CharSequence)localObject1);
switch (this.mStatus)
{
default:
localObject2 = "----";
this.mtvStatus.setText((CharSequence)localObject2);
switch (this.mPlugged)
{
default:
localObject3 = "----";
this.mtvPlugged.setText((CharSequence)localObject3);
if (this.mScale > 0)
{
this.mtvCap.setText(this.mRemainingCapacity + "% (" + this.mLevel + "/" + this.mScale + ")");
this.mtvVoltage.setText(this.mVoltage + "mV");
if (this.mTemperature > -9999)
break;
this.mtvTemperature.setText("----");
}
break;
case 1:
case 2:
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}
}
while (true)
{
Date localDate = new Date(this.mUpdateTime);
this.mtvLastUpdate.setText(this.mDF.format(localDate));
this.mtvPresent.setText(getResources().getText(R.string.info_text_battery_present_no));
this.mtvTechnology.setText("----");
localObject1 = getResources().getText(R.string.info_text_battery_health_unknown);
localObject1 = getResources().getText(R.string.info_text_battery_health_good);
localObject1 = getResources().getText(R.string.info_text_battery_health_overheat);
localObject1 = getResources().getText(R.string.info_text_battery_health_dead);
localObject1 = getResources().getText(R.string.info_text_battery_health_over_voltage);
localObject1 = getResources().getText(R.string.info_text_battery_health_unspecified_failure);
localObject2 = getResources().getText(R.string.info_text_battery_status_unknown);
localObject2 = getResources().getText(R.string.info_text_battery_status_charging);
localObject2 = getResources().getText(R.string.info_text_battery_status_discharging);
localObject2 = getResources().getText(R.string.info_text_battery_status_not_charging);
localObject2 = getResources().getText(R.string.info_text_battery_status_full);
localObject3 = getResources().getText(R.string.info_text_battery_plugged_ac);
localObject3 = getResources().getText(R.string.info_text_battery_plugged_usb);
this.mtvCap.setText(this.mLevel);
this.mtvTemperature.setText(this.mTemperature / 10 + "°C / " + (int)((320.0F + 9.0F * this.mTemperature / 5.0F) / 10.0F) + "°F");
}
}
private void updateWidgetDemo(Intent paramIntent)
{
this.mDrawer.update(paramIntent);
this.mivGraph.setImageBitmap(this.mDrawer.drawIcon(getResources().getDimensionPixelSize(R.dimen.workspaceicon_size), 1.0F));
}
@TargetApi(Build.VERSION_CODES.CUPCAKE) protected void onCreate(Bundle paramBundle)
{
this.mPM = PreferenceManager.getDefaultSharedPreferences(getApplication());
if (Integer.parseInt(this.mPM.getString("ICON_STYLE", getResources().getString(R.string.icon_style_default))) == 0)
{
setTheme(R.style.Theme_Transparent);
getWindow().setFlags(4, 4);
setContentView(R.layout.page_info_android);
}
while (true)
{
this.mDrawer = new IconDrawer(this);
this.mtvPresent = ((TextView)findViewById(R.id.Present));
this.mtvTechnology = ((TextView)findViewById(R.id.Tech));
this.mtvHealth = ((TextView)findViewById(R.id.Health));
this.mtvStatus = ((TextView)findViewById(R.id.Status));
this.mtvPlugged = ((TextView)findViewById(R.id.Plugged));
this.mtvCap = ((TextView)findViewById(R.id.Cap));
this.mtvVoltage = ((TextView)findViewById(R.id.Voltage));
this.mtvTemperature = ((TextView)findViewById(R.id.Temp));
this.mivGraph = ((ImageView)findViewById(R.id.ivGraph));
this.mtvLastUpdate = ((TextView)findViewById(R.id.tvLastUpdate));
this.mbtSettings = ((Button)findViewById(R.id.btSettings));
this.mbtSettings.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
Intent localIntent = new Intent(PageInfo.this.getApplicationContext(), PrefSettings.class);
PageInfo.this.startActivity(localIntent);
}
});
this.mDF = android.text.format.DateFormat.getTimeFormat(getBaseContext());
super.onCreate(paramBundle);
setTheme(R.style.Theme_NoTitle);
setContentView(R.layout.page_info);
return;
}
}
@TargetApi(Build.VERSION_CODES.CUPCAKE) protected void onPause()
{
this.mBatteryIR.unregisterReceiver();
this.mBatteryIR = null;
int[] arrayOfInt = AppWidgetManager.getInstance(this).getAppWidgetIds(new ComponentName(this, WidgetProvider.class));
if ((arrayOfInt != null) && (arrayOfInt.length > 0))
{
Intent localIntent = new Intent();
localIntent.putExtra("appWidgetIds", arrayOfInt);
localIntent.setAction(WidgetProvider.FORCE_WIDGET_UPDATE);
sendBroadcast(localIntent);
}
super.onPause();
}
protected void onResume()
{
if (this.mBatteryIR == null)
this.mBatteryIR = new BatteryInfoReceiver();
Intent localIntent = this.mBatteryIR.registerReceiver();
this.mBatteryIR.onReceive(getApplicationContext(), localIntent);
super.onResume();
}
class BatteryInfoReceiver extends BroadcastReceiver
{
BatteryInfoReceiver()
{
}
public void onReceive(Context paramContext, Intent paramIntent)
{
if ("android.intent.action.BATTERY_CHANGED".equals(paramIntent.getAction()))
PageInfo.this.updateInfo(paramContext, paramIntent);
}
public Intent registerReceiver()
{
return PageInfo.this.getApplicationContext().registerReceiver(this, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
}
public void unregisterReceiver()
{
PageInfo.this.getApplicationContext().unregisterReceiver(this);
}
}
}
如果有人帮助我,我将非常感激。 :)
答案 0 :(得分:1)
private int mLevel;
//...
this.mtvCap.setText(this.mLevel);
setText(int)
需要资源标识符,但mLevel
不是一个。请改用setText(CharSequence)
,例如
this.mtvCap.setText(Integer.toString(this.mLevel));
答案 1 :(得分:0)
1.在eclipse中,转到Project>清洁...
2.选择您的项目,然后按OK
3.启动应用
如果再次发生,请删除r.java文件。它会自动生成。
希望这有帮助。
修改: - 强>
我认为你的代码中的某个地方你尝试使用一些整数值来设置setText()。请调试,你会注意到这一点。
尝试, view.setText(Integer.toString(iSomeInteger))。
关注this link for better clearity
希望这有帮助。