0

My question is about Java/Android.

How can I call a non-static method from an constructor?

My code:

public class input extends TextView{
    public input(Context context, AttributeSet attr) {
        super(context, attr, getIdentifier()); //Error
    }
    
    public int getIdentifier(){
        if(getInputType() == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS){
            return Resources.getSystem().getIdentifier("editTextStyle","attr","android");
        }
        else{
            return Resources.getSystem().getIdentifier("textViewStyle","attr","android");
        }
    }
}

I read all stackoverflow questions related to my question but no one helps me because they offer only two solution: Create a new instance or set the method to call to static

In this case, I can't create a new instance, because the app would crash:

super(context, attr, new input(context,attr).getIdentifier());//App crashes

And in my case, I can't set my getIdentifier method to static because then I can't call inside the getInputType method, as you can see...

So how can I solve my problem?

user207421
  • 305,947
  • 44
  • 307
  • 483
Button
  • 19
  • 5
  • 1
    Cam you please show the constructor of `TextView` – akortex Aug 13 '21 at 09:12
  • 3
    Your requirement is to be able to call `getInputType()` at a time before the object is initialised. That's a bad idea. Depending on what `getInputType()` actually does, it is likely to return the wrong result. – khelwood Aug 13 '21 at 09:13
  • 1
    https://stackoverflow.com/questions/5230565/can-i-call-methods-in-constructor-in-java – JCompetence Aug 13 '21 at 09:14
  • 1
    make it static. – Maurice Perry Aug 13 '21 at 09:14
  • @MauricePerry I am going to go on a limp here and say that he can't because he is required to call `getInputType`. Unfortunately without sharing any more code, this is impossible to say for sure. – akortex Aug 13 '21 at 09:16
  • @akortex well this makes no sense because he cant call `getInputType` before the object was created. – Maurice Perry Aug 13 '21 at 09:18
  • @akortex there are the constructors of TextView: https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java – Button Aug 13 '21 at 09:18
  • @MauricePerry, well technically he can. Assuming that `TextView` allows for setting the identifier at latter time, he can call `super` and then call `getIdentifier`. Design wise, this is bad and complicated but it is doable. – akortex Aug 13 '21 at 09:19
  • @akortex What code do you need? – Button Aug 13 '21 at 09:33
  • @khelwood But I need to know the inputType to decide whether I show a TextView of an EditText... – Button Aug 13 '21 at 09:34
  • As your question is about *non*-static methods, the tag [tag:static-methods] is irrelevant. – user207421 Aug 13 '21 at 09:40
  • @khelwood And how would you solve this problem? Can I alternatively call super two times? – Button Aug 13 '21 at 09:44
  • @user207421 I thought maybe there are a way to get the inputType from an static method... – Button Aug 13 '21 at 09:45
  • If you must know that in order to construct your object, then you need to **figure it out in advance of calling the constructor**. You cannot decide it based on getting information from an uninitialised object; it just doesn't make sense. – khelwood Aug 13 '21 at 09:47
  • @khelwood I already tried to "figure it out in advance of calling the constructor" but it didn't work. If doesn't make sense, can I alternatively call super two times? Or what would you do? – Button Aug 13 '21 at 09:51
  • You cannot call super two times. If you tried to figure it out in advance and it didn't work, then you presumably have some code that you could post here and get help with. I don't know what `getInputType` does so I can't tell you how to solve it beyond what I've already said. – khelwood Aug 13 '21 at 09:53
  • @khelwood I already posted here some code (above). With getInputType you get the type of the editable content. For example of you set in Edittext (xml) the following: android:inputType="textEmailAddress". – Button Aug 13 '21 at 10:00

0 Answers0