-3

I'm a beginner in programming. I have a function outside of the activity that when the user types inside EditText the application displays an x to clear the form. When I call this function the error occurs: android.content.res.Resources android.content.Context.getResources () on a null object reference at android.content.ContextWrapper.getResources If I leave within the same class it works, however I will use this function a lot in other activities

I researched a lot but could not find a solution

Activity.java

public class Activity extends AppCompatActivity{

    EditText edtName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty);

        edtName = (EditText)findViewById(R.id.edt1);

        FormControl formcontrol = new FormControl();

        formcontrol.ajustaEditText(edtName);

        }

}

FormControl.java

public  class FormControl extends AppCompatActivity {



    public void ajustaEditText(final EditText et){

        String value = "";

        final Drawable x =  getResources().getDrawable( R.drawable.ic_action_navigation_close);

        x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());

        et.setCompoundDrawables(null, null, value.equals("") ? null : x, null);

        et.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (et.getCompoundDrawables()[2] == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth()) {
                    et.setText("");
                    et.setCompoundDrawables(null, null, null, null);
                }
                return false;
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null);
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
        });

    }


}
Aj 27
  • 2,316
  • 21
  • 29

4 Answers4

2

Your FormControl class extends an Activity which seems to be the problem here.

1 - Remove the extend relationship in you class.

public  class FormControl 

2 - Pass the context of current Activity of the FormControl

 private Context context;

 public FormControl(Context context){
 this.context = context 
}

3 - Get the resources from the current context.

 final Drawable x =  context.getResources().getDrawable( R.drawable.ic_action_navigation_close);

4 - Get the object of your FormClass

FormControl formcontrol = new FormControl(this);
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
1

this is not allowed.

    FormControl formcontrol = new FormControl();

    formcontrol.ajustaEditText(edtName);

you have start activity with an intent, pass data as intent bundle object. check example below.

How do I pass data between Activities in Android application?

Sush
  • 3,864
  • 2
  • 17
  • 35
0

FormControl is activity you can not create instance of the activity either you can get instance of activity.

public static FormControl instance;

public  class FormControl extends AppCompatActivity {

public static FormControl getInstance() {
        return instance;
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        instance = this;
}
}
Aj 27
  • 2,316
  • 21
  • 29
0

You will have to pass Context of the activity wherever you want to access getResources().

SO go like this...

Activity Class

public class Activity extends AppCompatActivity{

EditText edtName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acitivty);

    edtName = (EditText)findViewById(R.id.edt1);

    FormControl formcontrol = new FormControl();

    formcontrol.ajustaEditText(this, edtName);

    }
}

FormControl Class

public  class FormControl {



public void ajustaEditText(Context context,final EditText et){

    String value = "";

    final Drawable x =  context.getResources().getDrawable( R.drawable.ic_action_navigation_close);

    x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());

    et.setCompoundDrawables(null, null, value.equals("") ? null : x, null);

    et.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (et.getCompoundDrawables()[2] == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            if (event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth()) {
                et.setText("");
                et.setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
    });
    et.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null);
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    });
}}
buzzingsilently
  • 1,546
  • 3
  • 12
  • 18