gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:03:52 25 4
gpt4 key购买 nike

首先,我对 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/

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