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

时间:2015-02-23 08:08:15

标签: java android

我正在尝试验证我的文本框。我创建了一个具有验证功能的Java类。我正在使用setError(MSG,icon);

这样的事情:

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;
   }
 }

我的活动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){}
    });

我尝试了以下内容:

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

我收到以下错误:

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)

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:4)

无需单独传递Context以访问getResources()方法。因为已将EditText视图传递给hasText所以请使用editText.getContext()来调用getResources()方法:

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

答案 1 :(得分:0)

您可以在方法中采用额外的变量上下文,如下面的代码 并从此方法的活动传递活动上下文中调用该方法。

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;
   } 

答案 2 :(得分:0)

您可以覆盖Application类,将静态变量设置为gInstance中的应用程序实例Application.onCreate(),然后调用gInstance.getResources()

是的,静态变量很难看,但只要您的应用程序正在运行,您的应用程序对象将始终可用。一旦应用程序未运行,您将不再需要资源。