1

In my Android app, I want to use a single variable for the log name in multiple files. At the moment, I'm specifying it separately in each file, e.g.

public final String LOG_NAME = "LogName";
Log.d(LOG_NAME, "Logged output);

I've tried this:

public final String LOG_NAME = (String) getText(R.string.app_name_nospaces);

And while this works in generally most of my files, Eclipse complains about one of them:

The method getText(int) is undefined for the type DatabaseManager

I've made sure I'm definitely importing android.content.Context in that file. If I tell it exactly where to find getText:

Multiple markers at this line
- Cannot make a static reference to the non-static method getText(int) from the type Context
- The method getText(int) is undefined for the type DatabaseManager

I'm sure I've committed a glaringly obvious n00b error, but I just can't see it! Thanks for all help: if any other code snippets would help, let me know.

spookypeanut
  • 503
  • 1
  • 6
  • 24

2 Answers2

3

That's because getText is a method of Context. It does not matter if you import the Context class; what matters is that you invoke that method from a Context (for instance, the Activity class is a Context (it inherits Context)).

In that case, what I'd recommend, is creating a Application class that returns the context you want. Here I explain how to do it. After that you can do something like:

public final String LOG_NAME = (String) App.getContext().getText(R.string.app_name_nospaces);
Community
  • 1
  • 1
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks, that seems to work great, and I feel like I better understand what's going on now. The only place it doesn't work is in the application class itself: I thought I'd be able to just use getText there (as it's a context), but I get a NullPointerException. Any suggestions? – spookypeanut Jan 08 '11 at 17:18
  • Got it working, thanks to this question: http://stackoverflow.com/questions/4157228/nullpointerexception-trying-to-access-a-string-resource Turns out the resources aren't accessible until onCreate, so I've made my LOG_NAME static, rather than final, and set it inside onCreate rather than on declaration. – spookypeanut Jan 08 '11 at 17:43
0

Depending on what sort of 'files' you are using, you can define a TAG that is used.

For example, when I create an app, I like to create a base class for my Activity classes...

Suppose my app is called 'Wibble', and my package is com.mydomain.Wibble...I create my base Activity like so...

package com.mydomain.Wibble

public class WibbleActivity extends Activity {

    final protected String TAG = this.getClass().getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // I'll explain how this next line works later
        android.util.Log.d(TAG, "Entered onCreate()...");
    }
}

Now suppose I derive an activity as follows...

package com.mydomain.Wibble

public class SomeActivity extends WibbleActivity {

    @Override
    protexted void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Don't Log "Entered onCreate()..." - WibbleActivity does it for me
        android.util.Log.d(TAG, "SomeText");
    }
}

Then I derive another Activity...

package com.mydomain.Wibble

public class SomeOtherActivity extends WibbleActivity {

    @Override
    protexted void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Don't Log "Entered onCreate()..." - WibbleActivity does it for me
        android.util.Log.d(TAG, "SomeOtherText");
    }

When onCreate() is called for SomeActivity, the output will be...

com.mydomain.Wibble.SomeActivity    Entered onCreate()...
com.mydomain.Wibble.SomeActivity    SomeText

...when onCreate() is called for SomeOtherActivity however, the output will be...

com.mydomain.Wibble.SomeOtherActivity    Entered onCreate()...
com.mydomain.Wibble.SomeOtherActivity    SomeOtherText

Neither activity needs to know specifics through an explicit string and the package name is prefixed. Obviously it will only work in certain situations but I find it useful.

Squonk
  • 48,735
  • 19
  • 103
  • 135