Android Dynamically setting a 9-patch background o

2019-08-06 04:51发布

问题:

This question already has an answer here:

  • How to programmatically re-apply a 9-patch image to an ImageView? 5 answers

firstly thank you for taking the time to read this.

So I have created three 9-patch files which are working correctly as I can set them in XML on my registration form I've created and they look fine.

The first two are set using an XML file depending on whether the EditText state_focused = 'true' or if state_focused='false'. That is working absolutely fine.

However, I now want to set the background to be another different 9-patch file if the user hasn't input any text into that particular EditText. Below is the function in which I want to do it (etUsername is initialised properly as an EditText):

private Boolean areAllEditTextFilledIn() {
    Boolean result = true;
    if (username.length() == 0) {
        etUsername.setBackground(editTextError);
        result = false;
    }
    return result;
}

And here is the code I've tried to use to get the 9-patch into my code.

NinePatchDrawable editTextError = (NinePatchDrawable) getResources().getDrawable(R.drawable.edittext_not_filled_in); //Line 42 where null pointer exception occurs

However, running this gives me this result when my registration activity is being loaded

04-09 19:23:32.928: E/AndroidRuntime(14401): FATAL EXCEPTION: main
04-09 19:23:32.928: E/AndroidRuntime(14401): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cgas4.lboro.loco/cgas4.lboro.loco.Register}: java.lang.NullPointerException
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.os.Looper.loop(Looper.java:155)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread.main(ActivityThread.java:5485)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at dalvik.system.NativeStart.main(Native Method)
04-09 19:23:32.928: E/AndroidRuntime(14401): Caused by: java.lang.NullPointerException
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at cgas4.lboro.loco.Register.<init>(Register.java:42)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at java.lang.Class.newInstanceImpl(Native Method)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at java.lang.Class.newInstance(Class.java:1319)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.Instrumentation.newActivity(Instrumentation.java:1069)
04-09 19:23:32.928: E/AndroidRuntime(14401):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
04-09 19:23:32.928: E/AndroidRuntime(14401):    ... 11 more

The name of the 9-patch is edittext_not_filled_in.9.png

Let me know if there is any other information you need.

回答1:

It seems to me that you put this line:

NinePatchDrawable editTextError = (NinePatchDrawable) getResources().getDrawable(R.drawable.edittext_not_filled_in);

in the class body, where you declare the field. narrow it down to

NinePatchDrawable editTextError;

and then in onCreate put

editTextError = (NinePatchDrawable) getResources().getDrawable(R.drawable.edittext_not_filled_in);

This should solve your problem.