4

自动生成的代码部分出现错误:R.drawable。请提出错误的解决方案:

R.drawable.icon :- 图标无法解析或不是字段

拥有此错误的 .java 文件如下所示:

package net.learn2develop.UsingNotifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class DisplayNotifications extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //---get the notification ID for the notification; 
        // passed in by the MainActivity---
        int notifID = getIntent().getExtras().getInt("NotifID");

        //---PendingIntent to launch activity if the user selects 
        // the notification---
        Intent i = new Intent("net.learn2develop.AlarmDetails");
        i.putExtra("NotifID", notifID);  

        PendingIntent detailsIntent = 
            PendingIntent.getActivity(this, 0, i, 0);

        NotificationManager nm = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
        Notification notif = new Notification(
            R.drawable.icon,
            "Time's up!",
            System.currentTimeMillis());

        CharSequence from = "AlarmManager - Time's up!";
        CharSequence message = "This is your alert, courtesy of the AlarmManager";        
        notif.setLatestEventInfo(this, from, message, detailsIntent);

        //---100ms delay, vibrate for 250ms, pause for 100 ms and
        // then vibrate for 500ms---
        notif.vibrate = new long[] { 100, 250, 100, 500};        
        nm.notify(notifID, notif);
        //---destroy the activity---
        finish();
    }
}

清单文件也表明了这一点:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.learn2develop.UsingNotifications"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.VIBRATE"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        
        <activity android:name=".AlarmDetails" 
            android:label="Details of the alarm">
            <intent-filter>
                <action android:name="net.learn2develop.AlarmDetails" />
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter>
        </activity>

        <activity android:name=".DisplayNotification" >
            <intent-filter>
                <action android:name="net.learn2develop.DisplayNotification" />
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter>
        </activity>

    </application>
</manifest>
4

2 回答 2

3

I just thought I would add a quick additional answer to this topic. I am very new to Android development and had found that one of my classes was not compiling as it could not find any of my drawable attributes. In the end I tracked the problem down to the fact that the class was importing android.R (automatically added to the imports list by Eclipse). As soon as that line was taken out, the class compiled.

于 2013-04-23T17:49:46.157 回答
1

您应该检查 dir gen 下是否有一个名为 R.java 的文件。如果是这样打开它并检查是否有一个名为图标的属性。

可能是您移动了项目或从其他项目中复制了某些内容。在任何情况下,您都可以手动删除 gen 下的文件并让 Eclipse 重新创建它们。如果不是,您可以进入项目,然后选择您的项目。它应该工作。

于 2012-07-31T19:32:01.190 回答