繁体   English   中英

引用string.xml中的资源的运行时错误

[英]Runtime error referencing a resource in string.xml

我从string.xml引用资源时遇到Java运行时异常。 我在Android开发人员网站上阅读了“字符串资源”和“访问资源”,但仍然不知道为什么它们是错误的。 非常感谢你的帮助!

在string.xml中:

    <string name="fAudioRecorderDir">AudioRecorder</string>

在Java中:(这是上传的第26行,它具有NullPointerException)

 final String homeDir = Environment.getExternalStorageDirectory().getPath() + "/" + getResources().getString(R.string.fAudioRecorderDir) + "/";

从Logcat ##

E/AndroidRuntime(14597): FATAL EXCEPTION: main  
E/AndroidRuntime(14597): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.medikin.android.dictaphone/com.medikin.android.dictaphone.Upload}: java.lang.NullPointerException  
E/AndroidRuntime(14597):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)  
E/AndroidRuntime(14597):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)  
E/AndroidRuntime(14597):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)  
E/AndroidRuntime(14597):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)  
E/AndroidRuntime(14597):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)  
E/AndroidRuntime(14597):    at android.widget.TabHost.setCurrentTab(TabHost.java:326)  
E/AndroidRuntime(14597):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)  
E/AndroidRuntime(14597):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)  
E/AndroidRuntime(14597):    at android.view.View.performClick(View.java:2485)  
E/AndroidRuntime(14597):    at android.view.View$PerformClick.run(View.java:9080)  
E/AndroidRuntime(14597):    at android.os.Handler.handleCallback(Handler.java:587)  
E/AndroidRuntime(14597):    at android.os.Handler.dispatchMessage(Handler.java:92)  
E/AndroidRuntime(14597):    at android.os.Looper.loop(Looper.java:130)  
E/AndroidRuntime(14597):    at android.app.ActivityThread.main(ActivityThread.java:3683)  
E/AndroidRuntime(14597):    at java.lang.reflect.Method.invokeNative(Native Method)  
E/AndroidRuntime(14597):    at java.lang.reflect.Method.invoke(Method.java:507)  
E/AndroidRuntime(14597):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)  
E/AndroidRuntime(14597):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)  
E/AndroidRuntime(14597):    at dalvik.system.NativeStart.main(Native Method)  
E/AndroidRuntime(14597): Caused by: java.lang.NullPointerException  
E/AndroidRuntime(14597):    at android.content.ContextWrapper.getResources(ContextWrapper.java:80)  
E/AndroidRuntime(14597):    at com.medikin.android.dictaphone.Upload.<init>(Upload.java:26)  
E/AndroidRuntime(14597):    at java.lang.Class.newInstanceImpl(Native Method)  
E/AndroidRuntime(14597):    at java.lang.Class.newInstance(Class.java:1409)  
E/AndroidRuntime(14597):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)  
E/AndroidRuntime(14597):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)  
E/AndroidRuntime(14597):    ... 18 more  
W/ActivityManager(   68):   Force finishing activity com.medikin.android.dictaphone/.Dictaphone

我在这里猜测,但无论如何我都尝试一下(您应该添加更多代码):

  • getRessources在尝试获取其上下文时失败,我认为这是因为您的类没有从上下文继承(也许是一个单独的帮助器类)。

  • getRessources需要有效的上下文才能找到正确的res /目录。

两种选择:

  • 使此类成为您活动之一的子类 ,然后重试。

  • 将Context作为包含第26行的方法的参数传递。

即:

public void myMethod(Context cxt){
   String extStorage = Environment.getExternalStorageDirectory().getPath();
   String AudioRecorderDir=cxt.getResources().getString(R.string.fAudioRecorderDir);
   Log.d("SanityCheck","extStorage: "+extStorage+" audioRecoderDir: "+AudioRecorderDir);
   String homeDir=extStorage+"/"+AudioRecorderDir+"/";
   //etc
}

从您的活动中这样称呼它:

myObj.myMethod(this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM