Android,未找到字符串资源

Android,未找到字符串资源,android,string,resources,r.java-file,Android,String,Resources,R.java File,我正在开发一个应用程序,我发现了一些奇怪的错误。我在res/values/strings中放入了一些字符串资源并保存了它。现在,如果我想在活动中访问它,我会得到如下错误 07-13 11:16:20.050: ERROR/AndroidRuntime(5883): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060006 我不知道为什么。 资源如下所示: <stri

我正在开发一个应用程序,我发现了一些奇怪的错误。我在
res/values/strings
中放入了一些字符串资源并保存了它。现在,如果我想在活动中访问它,我会得到如下错误

07-13 11:16:20.050: ERROR/AndroidRuntime(5883): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060006
我不知道为什么。 资源如下所示:

<string name="dialog_download_text">please wait while downloading</string>
我还查看了R.java并找到了我的条目

public static final class string {
    public static final int dialog_download_cancel=0x7f060005;
    public static final int dialog_download_text=0x7f060007;
    public static final int dialog_download_title=0x7f060006;
}
我不知道该怎么办,因为我以前从未遇到过这样的问题。
请帮帮我。谢谢大家。

很可能有额外的
导入android.R则在那里找不到您的字符串。否则,请清理项目并重建它,再试一次,然后返回报告。

有时我会出现此错误(每两天一次),这是因为eclipse没有编译新代码,而是部署了旧的apk。 我会用螺丝钉把它修好

  • 在eclipse上执行清理(项目->清理)
  • 关闭模拟器
  • 通过从命令行运行
    adb kill server
    adb start server
    重新启动adb服务器

签出此线程,它修复了我的问题。

在eclipse中清理项目,然后尝试运行。如果这不能解决您的问题,请从已经运行的模拟器卸载该应用程序,然后从eclispe再次运行它。

我所做的是

导入android.R

而不是

导入com.example.project.R


android.R没有您的资源,它只有一般的android资源。

我目前正在编写“net.superlinux.tcltktutorials”,您现在可以在play store上找到它。 现在,在应用程序中,我想使用标准的语言环境使用方法,而不是我的语言环境使用方法。我必须添加阿拉伯语/res/vlaues-ar,而英语/res/values是默认值。 该应用程序是关于基于广告的YouTube播放列表TCL/Tk编程语言教程。现在播放列表可以是阿拉伯语和英语。我注意到,如果同一个播放列表的默认/res/values中有36个条目,而/res/values中有35个条目,这将导致ResourceNotFound异常。你所要做的就是在列表的底部添加一个空的缺失条目,使其在英语和阿拉伯语中的数字相等,即使英语播放列表中的数字较少

这是我添加到列表活动中形成的播放列表的方法,也是将资源作为xml内置数据使用的一种聪明方法:

package net.superlinux.tcltktutorials;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListOfVideos extends ListActivity {

List<String> model = new ArrayList<String>();
ArrayAdapter<String> adapter = null;
List<String> filename = new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);



     adapter = new ArrayAdapter<String> (this, R.layout.list_item, model);


    load_playlist();
    setListAdapter(adapter);



}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
    String selected_youtube_video=filename.get(position);
    try {Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+selected_youtube_video));
startActivity(i);
    }
    catch(Exception e){

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+selected_youtube_video+"&loop=1&autoplay=1")));
        e.printStackTrace();
    }
}

void load_playlist()
{

    int display_id=0;
    int file_id=0;
    //loop forever until nothing has to be added to the ListView or stop if the list item
    // to be added does not exist.
    for (int i=0;;i++){
        display_id=getResources().getIdentifier("display_"+i, "string", getPackageName());
    if (display_id!=0 && getString(display_id).length()!=0)
        adapter.add(getString(display_id));
    else {
        Log.e("string id not found or empty","R.string.display_"+i );
        return;
    }
    file_id=getResources().getIdentifier("file_"+i, "string", getPackageName());
    if (file_id!=0 && getString(file_id).length()!=0){
    filename.add(getString(file_id));
    }
    else {
        Log.e("string id not found or empty","R.string.file_"+i );
        return;
    }
    }
}

}
包net.superlinux.tcltktutorials;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类ListOfVideos扩展了ListActivity{
列表模型=新的ArrayList();
ArrayAdapter适配器=空;
列表文件名=新的ArrayList();
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
适配器=新阵列适配器(此,R.layout.list_项,型号);
加载播放列表();
setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
所选字符串\u youtube\u video=filename.get(位置);
试试{Intent i=newintent(Intent.ACTION_视图,Uri.parse(“vnd.youtube:+selected_youtube_视频));
星触觉(i);
}
捕获(例外e){
startActivity(新意图(Intent.ACTION_视图,Uri.parse(“http://www.youtube.com/watch?v=“+所选视频”+“&loop=1&autoplay=1”);
e、 printStackTrace();
}
}
无效加载\u播放列表()
{
int display_id=0;
int file_id=0;
//永远循环,直到不需要向ListView添加任何内容,或者如果列表项
//要添加的内容不存在。
对于(int i=0;i++){
display_id=getResources().getIdentifier(“display_”+i,“string”,getPackageName());
if(display_id!=0&&getString(display_id).length()!=0)
add(getString(display_id));
否则{
Log.e(“字符串id未找到或为空”,“R.string.display_u2;”+i);
返回;
}
file_id=getResources().getIdentifier(“file_u”+i,“string”,getPackageName());
if(file_id!=0&&getString(file_id).length()!=0){
add(getString(file_id));
}
否则{
Log.e(“字符串id未找到或为空”,“R.string.file_u2;”+i);
返回;
}
}
}
}

我也遇到了这个问题,一开始它非常复杂。 1:我确保将正确的R导入到我的类中 2:我检查了gen文件夹中的R.java,资源id就在那里。 3:我清理了我的项目,从手机中删除并重新启动它 例外仍然存在

最后我找到了原因: 我刚刚将我的资源字符串添加到特定于语言的文件夹(比如values zh)中的strings.xml中,但在default values文件夹中的default strings.xml中丢失了它


然后问题就解决了。

有两种情况下,您可能会遇到此错误

1.您的strings.xml文件引用不正确

Ans:根据您的应用程序为R.java文件提供正确的包名

2.如果您保持正确的string.xlm路径,您可能会得到错误

答:一旦清理并构建投影仪,请重新启动eclipse或android studio

谢谢
krishh

这是我第二次忘记检查这个疯狂的导入!,谢谢Elijah,这是android IDE的前一个版本,它基于Eclipse,不适用于android Studio。
package net.superlinux.tcltktutorials;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListOfVideos extends ListActivity {

List<String> model = new ArrayList<String>();
ArrayAdapter<String> adapter = null;
List<String> filename = new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);



     adapter = new ArrayAdapter<String> (this, R.layout.list_item, model);


    load_playlist();
    setListAdapter(adapter);



}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
    String selected_youtube_video=filename.get(position);
    try {Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+selected_youtube_video));
startActivity(i);
    }
    catch(Exception e){

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+selected_youtube_video+"&loop=1&autoplay=1")));
        e.printStackTrace();
    }
}

void load_playlist()
{

    int display_id=0;
    int file_id=0;
    //loop forever until nothing has to be added to the ListView or stop if the list item
    // to be added does not exist.
    for (int i=0;;i++){
        display_id=getResources().getIdentifier("display_"+i, "string", getPackageName());
    if (display_id!=0 && getString(display_id).length()!=0)
        adapter.add(getString(display_id));
    else {
        Log.e("string id not found or empty","R.string.display_"+i );
        return;
    }
    file_id=getResources().getIdentifier("file_"+i, "string", getPackageName());
    if (file_id!=0 && getString(file_id).length()!=0){
    filename.add(getString(file_id));
    }
    else {
        Log.e("string id not found or empty","R.string.file_"+i );
        return;
    }
    }
}

}