简体   繁体   English

如何将getResources()用于非活动类

[英]How to use getResources() for non-activity class

I am trying to validate my text box. 我正在尝试验证我的文本框。 I have created a Java class which has the validation. 我创建了一个具有验证的Java类。 I am using setError(MSG, icon); 我正在使用setError(MSG,icon);

Something like this: 像这样:

public class Validate
{

public static boolean hasText(EditText editText) 
{
    String text = editText.getText().toString().trim();
    editText.setError(null);

    if (text.length() == 0) 
    {
        errorIcon = context.getResources().getDrawable(R.drawable.alert);
        errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
        editText.setError((REQUIRED_MSG), errorIcon);
        return false;
    }

    return true;
   }
 }

And my activity java file has this: 我的活动Java文件具有以下内容:

    @Override
protected void onCreate(Bundle savedInstanceState) 
{       
        fname =(EditText)findViewById(R.id.fname);
        fname.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) 
        {
            Validate.hasText(fname);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

I tried the following: 我尝试了以下方法:

public Validate(Context context) 
{
  this.context = context;
 }
}

I get the following error: 我收到以下错误:

02-23 13:30:58.718: E/AndroidRuntime(1334): FATAL EXCEPTION: main
02-23 13:30:58.718: E/AndroidRuntime(1334): java.lang.NullPointerException
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.zin.testText.Text_Validate.hasText(Text_Validate.java:65)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.zin.testText.Text_Form$1.afterTextChanged(Text_Form.java:107)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7320)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9073)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:673)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.os.Looper.loop(Looper.java:137)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at java.lang.reflect.Method.invoke(Method.java:525)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 13:30:58.718: E/AndroidRuntime(1334):     at dalvik.system.NativeStart.main(Native Method)

Can somebody help me to fix this? 有人可以帮我解决这个问题吗?

No need to pass Context separately to access getResources() method. 无需单独传递Context即可访问getResources()方法。 Because already passing EditText view to hasText so use editText.getContext() to call getResources() method: 因为已经将EditText视图传递给hasText所以请使用editText.getContext()调用getResources()方法:

errorIcon = editText.getContext().getResources().getDrawable(R.drawable.alert);

you can take a extra variable context in your method like below code and call that method from your activity pass activity context on this method. 您可以像下面的代码一样在方法中使用额外的变量上下文,然后从您的活动传递活动上下文的方法中调用该方法。

public static boolean hasText(EditText editText ,Context context) 
{
    String text = editText.getText().toString().trim();
    editText.setError(null);

    if (text.length() == 0) 
    {
        errorIcon = context.getResources().getDrawable(R.drawable.alert);
        errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
        editText.setError((REQUIRED_MSG), errorIcon);
        return false;
    }

    return true;
   } 

You can override Application class, set a static variable to the application instance gInstance in Application.onCreate() and then call gInstance.getResources() . 您可以覆盖Application类,在Application.onCreate()为应用程序实例gInstance设置静态变量,然后调用gInstance.getResources()

Yes, the static variable thing is ugly, but you application object will always be available as long as your app is running. 是的,静态变量很丑陋,但是只要您的应用程序正在运行,您的应用程序对象将始终可用。 As soon as the app is not running, you won't need resources any more. 该应用程序不运行后,您将不再需要任何资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM