简体   繁体   中英

string.xml in Android library

I am working in a library for android apps, and I am trying to use the string.xml to put some message in key points for the users.

My problem is when I test the library in an android testing app to see the workflow and reach the point where am I displaying a message (in this case in the logs) that occurs a fatal error and the app crash.

I am thinking that collide with the string.xml from the testing app and overwrite the string.xml that I have in the library. Can this be solved in some way or I have forcibly use the testing app string.xml to workout with the library??

Do i have to make my class extends from Activity for use getResources()? or is any other way to get access to string.xml, I will leave the code and the logs.

Code

public void createCertificateLogTxt(){
    File certificado = new File (this.KeyFolderContainer.getAbsoluteFile(),"certificadoLog.txt");
    if(!certificado.exists()){
        try {
            certificado.createNewFile();
            this.CertificadoLog = certificado;
            Log.i(LOGFILE, "certificadoLog.txt Creado"+ this.getResources().getString(R.string.app_name));
        } catch (IOException e) {
            Log.e(LOGFILE, "No se puede crear certificadoLog.txt"+ R.string.LOGFILE, e);
        }
    }

Logs

09-14 02:41:24.032: I/Choreographer(23748): Skipped 105 frames!  The application may be doing too much work on its main thread.
09-14 02:41:25.593: D/AndroidRuntime(23748): Shutting down VM
09-14 02:41:25.593: W/dalvikvm(23748): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
09-14 02:41:25.753: E/AndroidRuntime(23748): FATAL EXCEPTION: main
09-14 02:41:25.753: E/AndroidRuntime(23748): java.lang.IllegalStateException: Could not execute method of the activity
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.view.View$1.onClick(View.java:3633)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.view.View.performClick(View.java:4240)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.view.View$PerformClick.run(View.java:17721)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.os.Handler.handleCallback(Handler.java:730)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.os.Looper.loop(Looper.java:137)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.app.ActivityThread.main(ActivityThread.java:5103)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at java.lang.reflect.Method.invokeNative(Native Method)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at java.lang.reflect.Method.invoke(Method.java:525)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at dalvik.system.NativeStart.main(Native Method)
09-14 02:41:25.753: E/AndroidRuntime(23748): Caused by: java.lang.reflect.InvocationTargetException
09-14 02:41:25.753: E/AndroidRuntime(23748):    at java.lang.reflect.Method.invokeNative(Native Method)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at java.lang.reflect.Method.invoke(Method.java:525)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.view.View$1.onClick(View.java:3628)
09-14 02:41:25.753: E/AndroidRuntime(23748):    ... 11 more
09-14 02:41:25.753: E/AndroidRuntime(23748): Caused by: java.lang.NullPointerException
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.idyseg.movilsecure.files.KeyFileManager.createCertificateLogTxt(KeyFileManager.java:76)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.idyseg.movilsecure.MSMasterControler.<init>(MSMasterControler.java:34)
09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.idyseg.librerytester.MainActivity.Conector(MainActivity.java:33)
09-14 02:41:25.753: E/AndroidRuntime(23748):    ... 14 more
09-14 02:41:28.253: I/Process(23748): Sending signal. PID: 23748 SIG: 9

I think the problem -- or at least part of it -- is that you're trying to call this.getResources() during your Activity's constructor, as shown by this line in the stack trace:

09-14 02:41:25.753: E/AndroidRuntime(23748):    at com.idyseg.movilsecure.MSMasterControler.<init>(MSMasterControler.java:34)

As far as I know, the Resources are not available until the Activity's onCreate method has been called. Try moving the call to createCertificateLogTxt to your Activity's onCreate method.

Also, if you defined the KeyFileManager class as an Activity just so that you could invoke getResources on it, then that's also part of the problem: If it's not being used as an Activity (that is, if it doesn't use the standard Activity lifecyle methods), then once again the Resources won't be available through this class. In this case, you want to modify KeyFileManager.createCertificateLogTxt() to pass the main Activity's Context as a parameter:

KeyFileManager.createCertificateLogTxt(Context context) {
   //....
              Log.i(LOGFILE, "certificadoLog.txt Creado"+ context.getResources().getString(R.string.app_name));
}

And by the way: although it's not important, this line:

        Log.e(LOGFILE, "No se puede crear certificadoLog.txt"+ R.string.LOGFILE, e);

looks suspicious to me; don't you want something like this?

        Log.e(LOGFILE, "No se puede crear certificadoLog.txt " + getString(R.string.LOGFILE), e);

I know this question has been asked few years back, but recently due to advancements in Android Studio, I found a more feasible solution.

When creating module, most of the time, we forget to update manifest of module, since we rarely use it when there is no <activity> present in our module for our use case. But, if we want to use a resource file like strings.xml or drawables , we need to define package in our Manifest, otherwise, we cannot use any resources in res directory of the module.

Like this,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourpackagename">      <!-- this line -->

</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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