0

In an Android project I needed a way to retrieve a string value not from a Context but from a static method.

For this, I've implemented the solution How can I get a resource content from a static context? and I've added to the application tag the android:name attribute:

<application android:label="@string/app_name" android:allowBackup="true" android:name=".App">

Now, the application starts, but throws a NullPointerException when executing the following line of code in the OnCreate() of a secondary page:

findViewById(R.layout.level).setDrawingCacheEnabled(true);

The important part is R.layout.level ("level" is the name of the document that describes the page, level.xml), this object seems to be null now. Here the stack trace:

23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime  FATAL EXCEPTION: main
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.UpMap/com.UpMap.LevelActivity}: java.lang.NullPointerException
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread.access$2300(ActivityThread.java:125)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:123)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:4627)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:521)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime  Caused by: java.lang.NullPointerException
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at com.UpMap.LevelActivity.onCreate(LevelActivity.java:64)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
23:42:33.255    3550    com.UpMap   ERROR   AndroidRuntime      ... 11 more

How can I fix this? The original solution seems ok for most of the people, but not for me! Is that something related to the android:name attribute?

Community
  • 1
  • 1
TheUnexpected
  • 3,077
  • 6
  • 32
  • 62

2 Answers2

0

You need to inflate the layout before using its resources.

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout mFrame = (RelativeLayout)inflater.inflate(R.layout.level, null);   
//Now you can use/reference its resources/views etc.
mFrame.setDrawingCacheEnabled(true);
Manu
  • 81
  • 1
  • 5
  • It gaves me `android.view.InflateException: Binary XML file line #31: Error inflating class fragment` when executing the inflate method – TheUnexpected Aug 13 '13 at 22:26
0

I've solved by myself in this way: instead of referencing R.layout.level, I've given an ID to the root layout of the LevelActivity, with android:id="@+id/LevelLayout". Then I've changed the line into:

 findViewById(R.id.LevelLayout).setDrawingCacheEnabled(true);

Now it works

TheUnexpected
  • 3,077
  • 6
  • 32
  • 62