获取字段[]。getName()作为可访问的字符串?

时间:2014-11-19 13:12:40

标签: android string

我正在开发一个小函数,它从'raw'文件夹中加载所有文件,然后根据前缀确定是否应将它们保存在Vector中。这是为了准备我想要用于我正在制作的游戏的“水平加载”机制。我的功能是:

public void LoadRaws(){
    Field[] fields=R.raw.class.getFields();
    for(int count=0; count < fields.length; count++){
        StringStorage.add(fields[count].getName());
    }

    for(int i = 0; i < StringStorage.size(); i++){
        Log.i("Asset", StringStorage.get(i));
    }
}

Log.i();函数巧妙地将文件名打印到我的LogCat。但是,当我尝试类似的东西时:

for(int i = 0; i < StringStorage.size()-1; i++){
    Message(StringStorage.get(i));
}

要推送包含String的消息框,我会得到一个nullPointerException。这是日志:

11-19 14:08:51.881: E/AndroidRuntime(4161): FATAL EXCEPTION: main
11-19 14:08:51.881: E/AndroidRuntime(4161): Process: com.example.whoops, PID: 4161
11-19 14:08:51.881: E/AndroidRuntime(4161): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whoops/com.example.whoops.Game}: java.lang.NullPointerException
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread.access$800(ActivityThread.java:153)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.os.Looper.loop(Looper.java:157)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread.main(ActivityThread.java:5633)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at  java.lang.reflect.Method.invoke(Method.java:515)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at dalvik.system.NativeStart.main(Native Method)
11-19 14:08:51.881: E/AndroidRuntime(4161): Caused by: java.lang.NullPointerException
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:104)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at com.example.whoops.UtilLib.Message(UtilLib.java:186)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at com.example.whoops.LevelStorage.LoadRaws(LevelStorage.java:35)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at com.example.whoops.Game.onCreate(Game.java:55)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.Activity.performCreate(Activity.java:5312)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
11-19 14:08:51.881: E/AndroidRuntime(4161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
11-19 14:08:51.881: E/AndroidRuntime(4161):     ... 11 more

我不知道为什么会这样做,因为在定义这些文件的ID之前,我必须先获取名称。有谁知道我为什么会收到此错误?我该如何解决呢?

-Zubaja

2 个答案:

答案 0 :(得分:0)

尝试

for(int i = 0; i < StringStorage.size()-1; i++){
try{
     Message(StringStorage.get(i));
   }catch(Exception e){
    Log.i("Error at Index :",""+i);
   }
} 

你可以捕获空指针并检测其确切位置。

答案 1 :(得分:-1)

我自己设法解决了这个问题,以下是:

public void LoadRaws(){
    Field[] fields=R.raw.class.getFields();
    for(int count=0; count < fields.length; count++){
        StringStorage.add(fields[count].getName());
    }
}

'LevelStorage'的上述功能运行正常。但是,当尝试从同一个类访问Vector内部的数据时,它会给我一个错误。 然后我从我的'Game'类中调用了函数,然后尝试循环遍历向量,猜猜发生了什么?我得到了文件名。 感谢帮助我的每个人,你最好!