java - 为什么我无法访问drawable下的xml文件?

标签 java android

首先,我对 Android 非常陌生,我对它了解不多。我正在尝试使用它,我现在正在遵循本教程: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

我创建了一个类,如下

package com.example.tabbedactivity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabbedActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabbed);

        TabHost tabHost = getTabHost();

        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, PhotosActivity.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("Songs");
        songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, SongsActivity.class);
        songspec.setContent(songsIntent);

        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, VideosActivity.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(videospec); // Adding videos ta
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_tabbed, menu);
        return true;
    }

}

因此我在drawable下制作了三个xml文件,如下所示 icon_photos_tab.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/photo-hover"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/photo-unhover" />
</selector>

icon_songs_tab.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/music-hover"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/music-unhover" />
</selector>

icon_videos_tab.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/video-hover"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/video-unhover" />
</selector>

现在,当我尝试访问可绘制的这些 xml 文件时,IDE 显示问题正是下面显示的代码行,这些代码是 TabbedActivity.java 的一部分(我上面显示的类)

TabSpec songspec = tabHost.newTabSpec("Songs");
            songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));


TabSpec photospec = tabHost.newTabSpec("Photos");
            // setting Title and Icon for the Tab
            photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));


TabSpec videospec = tabHost.newTabSpec("Videos");
            videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));

错误提示

Multiple markers at this line - icon_photos_tab cannot be resolved or is not a field - R.drawable cannot be resolved to a variable

到底可能是什么问题。(我对 android 真的很陌生。这有点像我的第一个应用程序,我还没有经历过任何理论)

更新 我在控制台上收到以下错误

[2013-01-12 12:32:02 - TabbedActivity] res\drawable-ldpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-mdpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-xhdpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-hdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-ldpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-mdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-xhdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]

最佳答案

Android资源文件名不应包含'-'或大写字母。上述错误表明文件名包含'-'字符。因此,将 '-' 更改为分数 '_' 下的内容。像这样更改您的可绘制图像文件。

关于java - 为什么我无法访问drawable下的xml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290935/

相关文章:

java - Flink Scala ClassNotFoundException : org. apache.flink.api.common.typeinfo.TypeInformation

android - 在 Android 上使用 PowerMockito 模拟静态方法

Android - 忽略元素的简单框架解析器?

android - Dagger v2 : Inject 2 different scopes into one object

android - 在 Android Auto 中从 MusicBrowserServiceCompat 发送 API 请求

android - 应用程序类 onCreate 方法未在第二次启动应用程序时运行

java - 无法在 64 位 JVM 上加载 32 位 SWT 库

java - 在 Java 中计算 alpha 形状(凹包)

java 8 Stream - 如何在映射后获取原始对象引用

JavaFX 仅当它们是 :enabled 时才将 css 应用于 TableRows