问题描述
首先感谢您抽出宝贵的时间阅读本文章.
firstly thank you for taking the time to read this.
所以我创建了三个9补丁文件,它们可以正常工作,因为我可以在创建的注册表单上以XML设置它们,而且看起来不错.
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.
前两个是使用XML文件设置的,具体取决于EditText state_focused ='true'还是state_focused ='false'.一切正常.
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.
但是,如果用户尚未在该特定EditText中输入任何文本,我现在想将背景设置为另一个9补丁文件.下面是我要执行的功能(etUsername已正确初始化为EditText):
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;
}
这是我试图用来将9补丁添加到我的代码中的代码.
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
9个补丁的名称是edittext_not_filled_in.9.png
The name of the 9-patch is edittext_not_filled_in.9.png
让我知道您是否需要其他信息.
Let me know if there is any other information you need.
推荐答案
在我看来,您把这一行放在了上面:
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;
然后在onCreate中放置
and then in onCreate put
editTextError = (NinePatchDrawable) getResources().getDrawable(R.drawable.edittext_not_filled_in);
这应该可以解决您的问题.
This should solve your problem.
这篇关于Android在EditText小部件上动态设置9补丁背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!