Issue
I am trying to get String app_name from resources but I keep getting a null object reference. How can I fix it?
package com.testandroid;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import java.util.HashMap;
public class Test extends Activity {
public Test() {
try {
String hello = getString(R.string.app_name);
} catch (Exception e) {
Log.d("myLogs", e.getMessage());
}
}
}
<resources>
<string name = "app_name">Name</string>
</resources>
Solution
Ok, the easiest way is to use Context.
public class Test extends Activity {
public Test(Context context) {
try {
String hello = context.getResources().getString(R.string.app_name)
} catch (Exception e) {
Log.d("myLogs", e.getMessage());
}
}
}
Answered By - Ihor Salnikov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.