Call a non-static method inside an abstract class from a static method





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to detect when the input method picker called by InputMethodManager.showInputMethodPicker() is closed or changed. I found a possible solution proposed by Sherif elKhatib in another question: How can I tell if the input method picker is open or closed?. His answer suggests that the OP should use an abstract non-static class. However, I don't know how to call a method from the abstract class in a static method. I thought I'd open a separate question for it here because the original question is already old and inactive.



Here is the solution introduced by Sherif:



public abstract class InputMethodActivity extends FragmentActivity {
protected abstract void onInputMethodPicked();

@Override
protected void onCreate(Bundle savedInstanceState) {
mState = NONE;
super.onCreate(savedInstanceState);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == PICKING) {
mState = CHOSEN;
}
else if(mState == CHOSEN) {
onInputMethodPicked();
}
}

private static final int NONE = 0;
private static final int PICKING = 1;
private static final int CHOSEN = 2;
private int mState;
protected final void pickInput() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
mState = PICKING;
}
}


The method I'd like to call is pickInput() in order to get a response from onInputMethodPicked().



However, simply called pickInput(); from a static method doesn't work, and won't even find it.



Furthermore, InputMethodActivity.pickInput(); gives the error "Non-static method 'pickInput()' cannot be referenced from a static context".



Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); gives the error "'InputMethodActivity' is abstract; cannot be instantiated".



After further reading, I tried to create an anomynous class: InputMethodActivity anonym = new InputMethodActivity() {};, but this gives the error "Class 'Anonymous class derived from InputMethodActivity' must either be declared abstract or implement abstract method 'onInputMethodPicked()' in 'InputMethodActivity'". I thought both of them were already declared abstract, so I'm nearing the end of my ropes here.



Problem:



Basically, I would like to know if it's possible to run pickInput() in a static method, such as a public void onClick_TextView(View v){}, and how it can be achieved.










share|improve this question

























  • please post the code of your class where you want to call this method

    – Amrdroid
    Jan 3 at 7:16











  • and make sure you inherit from this abstract activity

    – Amrdroid
    Jan 3 at 7:26











  • @Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

    – Erlend K.H.
    Jan 3 at 9:09













  • see my answer @Erlend K.H.

    – Amrdroid
    Jan 3 at 9:50


















0















I'm trying to detect when the input method picker called by InputMethodManager.showInputMethodPicker() is closed or changed. I found a possible solution proposed by Sherif elKhatib in another question: How can I tell if the input method picker is open or closed?. His answer suggests that the OP should use an abstract non-static class. However, I don't know how to call a method from the abstract class in a static method. I thought I'd open a separate question for it here because the original question is already old and inactive.



Here is the solution introduced by Sherif:



public abstract class InputMethodActivity extends FragmentActivity {
protected abstract void onInputMethodPicked();

@Override
protected void onCreate(Bundle savedInstanceState) {
mState = NONE;
super.onCreate(savedInstanceState);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == PICKING) {
mState = CHOSEN;
}
else if(mState == CHOSEN) {
onInputMethodPicked();
}
}

private static final int NONE = 0;
private static final int PICKING = 1;
private static final int CHOSEN = 2;
private int mState;
protected final void pickInput() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
mState = PICKING;
}
}


The method I'd like to call is pickInput() in order to get a response from onInputMethodPicked().



However, simply called pickInput(); from a static method doesn't work, and won't even find it.



Furthermore, InputMethodActivity.pickInput(); gives the error "Non-static method 'pickInput()' cannot be referenced from a static context".



Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); gives the error "'InputMethodActivity' is abstract; cannot be instantiated".



After further reading, I tried to create an anomynous class: InputMethodActivity anonym = new InputMethodActivity() {};, but this gives the error "Class 'Anonymous class derived from InputMethodActivity' must either be declared abstract or implement abstract method 'onInputMethodPicked()' in 'InputMethodActivity'". I thought both of them were already declared abstract, so I'm nearing the end of my ropes here.



Problem:



Basically, I would like to know if it's possible to run pickInput() in a static method, such as a public void onClick_TextView(View v){}, and how it can be achieved.










share|improve this question

























  • please post the code of your class where you want to call this method

    – Amrdroid
    Jan 3 at 7:16











  • and make sure you inherit from this abstract activity

    – Amrdroid
    Jan 3 at 7:26











  • @Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

    – Erlend K.H.
    Jan 3 at 9:09













  • see my answer @Erlend K.H.

    – Amrdroid
    Jan 3 at 9:50














0












0








0


1






I'm trying to detect when the input method picker called by InputMethodManager.showInputMethodPicker() is closed or changed. I found a possible solution proposed by Sherif elKhatib in another question: How can I tell if the input method picker is open or closed?. His answer suggests that the OP should use an abstract non-static class. However, I don't know how to call a method from the abstract class in a static method. I thought I'd open a separate question for it here because the original question is already old and inactive.



Here is the solution introduced by Sherif:



public abstract class InputMethodActivity extends FragmentActivity {
protected abstract void onInputMethodPicked();

@Override
protected void onCreate(Bundle savedInstanceState) {
mState = NONE;
super.onCreate(savedInstanceState);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == PICKING) {
mState = CHOSEN;
}
else if(mState == CHOSEN) {
onInputMethodPicked();
}
}

private static final int NONE = 0;
private static final int PICKING = 1;
private static final int CHOSEN = 2;
private int mState;
protected final void pickInput() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
mState = PICKING;
}
}


The method I'd like to call is pickInput() in order to get a response from onInputMethodPicked().



However, simply called pickInput(); from a static method doesn't work, and won't even find it.



Furthermore, InputMethodActivity.pickInput(); gives the error "Non-static method 'pickInput()' cannot be referenced from a static context".



Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); gives the error "'InputMethodActivity' is abstract; cannot be instantiated".



After further reading, I tried to create an anomynous class: InputMethodActivity anonym = new InputMethodActivity() {};, but this gives the error "Class 'Anonymous class derived from InputMethodActivity' must either be declared abstract or implement abstract method 'onInputMethodPicked()' in 'InputMethodActivity'". I thought both of them were already declared abstract, so I'm nearing the end of my ropes here.



Problem:



Basically, I would like to know if it's possible to run pickInput() in a static method, such as a public void onClick_TextView(View v){}, and how it can be achieved.










share|improve this question
















I'm trying to detect when the input method picker called by InputMethodManager.showInputMethodPicker() is closed or changed. I found a possible solution proposed by Sherif elKhatib in another question: How can I tell if the input method picker is open or closed?. His answer suggests that the OP should use an abstract non-static class. However, I don't know how to call a method from the abstract class in a static method. I thought I'd open a separate question for it here because the original question is already old and inactive.



Here is the solution introduced by Sherif:



public abstract class InputMethodActivity extends FragmentActivity {
protected abstract void onInputMethodPicked();

@Override
protected void onCreate(Bundle savedInstanceState) {
mState = NONE;
super.onCreate(savedInstanceState);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == PICKING) {
mState = CHOSEN;
}
else if(mState == CHOSEN) {
onInputMethodPicked();
}
}

private static final int NONE = 0;
private static final int PICKING = 1;
private static final int CHOSEN = 2;
private int mState;
protected final void pickInput() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
mState = PICKING;
}
}


The method I'd like to call is pickInput() in order to get a response from onInputMethodPicked().



However, simply called pickInput(); from a static method doesn't work, and won't even find it.



Furthermore, InputMethodActivity.pickInput(); gives the error "Non-static method 'pickInput()' cannot be referenced from a static context".



Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); gives the error "'InputMethodActivity' is abstract; cannot be instantiated".



After further reading, I tried to create an anomynous class: InputMethodActivity anonym = new InputMethodActivity() {};, but this gives the error "Class 'Anonymous class derived from InputMethodActivity' must either be declared abstract or implement abstract method 'onInputMethodPicked()' in 'InputMethodActivity'". I thought both of them were already declared abstract, so I'm nearing the end of my ropes here.



Problem:



Basically, I would like to know if it's possible to run pickInput() in a static method, such as a public void onClick_TextView(View v){}, and how it can be achieved.







java android methods static abstract-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:39







Erlend K.H.

















asked Jan 3 at 6:24









Erlend K.H.Erlend K.H.

135114




135114













  • please post the code of your class where you want to call this method

    – Amrdroid
    Jan 3 at 7:16











  • and make sure you inherit from this abstract activity

    – Amrdroid
    Jan 3 at 7:26











  • @Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

    – Erlend K.H.
    Jan 3 at 9:09













  • see my answer @Erlend K.H.

    – Amrdroid
    Jan 3 at 9:50



















  • please post the code of your class where you want to call this method

    – Amrdroid
    Jan 3 at 7:16











  • and make sure you inherit from this abstract activity

    – Amrdroid
    Jan 3 at 7:26











  • @Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

    – Erlend K.H.
    Jan 3 at 9:09













  • see my answer @Erlend K.H.

    – Amrdroid
    Jan 3 at 9:50

















please post the code of your class where you want to call this method

– Amrdroid
Jan 3 at 7:16





please post the code of your class where you want to call this method

– Amrdroid
Jan 3 at 7:16













and make sure you inherit from this abstract activity

– Amrdroid
Jan 3 at 7:26





and make sure you inherit from this abstract activity

– Amrdroid
Jan 3 at 7:26













@Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

– Erlend K.H.
Jan 3 at 9:09







@Amrnoid I added the code inside public class MainActivity extends AppCompatActivity implements View.OnTouchListener {}. Could you please explain how to inherit from the abstract activity? Sorry, my java knowledge is somewhat limited.

– Erlend K.H.
Jan 3 at 9:09















see my answer @Erlend K.H.

– Amrdroid
Jan 3 at 9:50





see my answer @Erlend K.H.

– Amrdroid
Jan 3 at 9:50












1 Answer
1






active

oldest

votes


















0














abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes
and by inheritance you can use any method from InputMethodActivity (parent class) in
MainActivity (child class) like pickInput method



   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
@Override
protected void onInputMethodPicked(){
// your implemetion after pick
}
@Override
public void onClick(View view) {
pickInput();
}

}





share|improve this answer


























  • Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

    – Erlend K.H.
    Jan 3 at 10:33











  • if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

    – Amrdroid
    Jan 3 at 10:42













  • java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

    – Amrdroid
    Jan 3 at 10:44














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017293%2fcall-a-non-static-method-inside-an-abstract-class-from-a-static-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes
and by inheritance you can use any method from InputMethodActivity (parent class) in
MainActivity (child class) like pickInput method



   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
@Override
protected void onInputMethodPicked(){
// your implemetion after pick
}
@Override
public void onClick(View view) {
pickInput();
}

}





share|improve this answer


























  • Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

    – Erlend K.H.
    Jan 3 at 10:33











  • if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

    – Amrdroid
    Jan 3 at 10:42













  • java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

    – Amrdroid
    Jan 3 at 10:44


















0














abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes
and by inheritance you can use any method from InputMethodActivity (parent class) in
MainActivity (child class) like pickInput method



   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
@Override
protected void onInputMethodPicked(){
// your implemetion after pick
}
@Override
public void onClick(View view) {
pickInput();
}

}





share|improve this answer


























  • Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

    – Erlend K.H.
    Jan 3 at 10:33











  • if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

    – Amrdroid
    Jan 3 at 10:42













  • java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

    – Amrdroid
    Jan 3 at 10:44
















0












0








0







abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes
and by inheritance you can use any method from InputMethodActivity (parent class) in
MainActivity (child class) like pickInput method



   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
@Override
protected void onInputMethodPicked(){
// your implemetion after pick
}
@Override
public void onClick(View view) {
pickInput();
}

}





share|improve this answer















abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes
and by inheritance you can use any method from InputMethodActivity (parent class) in
MainActivity (child class) like pickInput method



   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
@Override
protected void onInputMethodPicked(){
// your implemetion after pick
}
@Override
public void onClick(View view) {
pickInput();
}

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 10:06

























answered Jan 3 at 9:47









AmrdroidAmrdroid

318110




318110













  • Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

    – Erlend K.H.
    Jan 3 at 10:33











  • if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

    – Amrdroid
    Jan 3 at 10:42













  • java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

    – Amrdroid
    Jan 3 at 10:44





















  • Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

    – Erlend K.H.
    Jan 3 at 10:33











  • if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

    – Amrdroid
    Jan 3 at 10:42













  • java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

    – Amrdroid
    Jan 3 at 10:44



















Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

– Erlend K.H.
Jan 3 at 10:33





Thanks for the suggestion, Amrnoid, but if I change public class MainActivity extends AppCompatActivity implements View.OnTouchListener {} to public class MainActivity extends InputMethodActivity implements View.OnTouchListener {}, I get a lot of new errors since other parts of the code require AppCompatActivity. However, I think we are close to making it work. Is there a way to extend MainActivity to both AppCompatActivity and InputMethodActivity?

– Erlend K.H.
Jan 3 at 10:33













if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

– Amrdroid
Jan 3 at 10:42







if A class inherit from B class and B class inherit from C, C class is parent of A class by default then AppCompatActivity is parent class of MainActivity

– Amrdroid
Jan 3 at 10:42















java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

– Amrdroid
Jan 3 at 10:44







java doesn’t provide support for multiple inheritance in classes so you can't extend MainActivity to both AppCompatActivity and InputMethodActivity

– Amrdroid
Jan 3 at 10:44






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017293%2fcall-a-non-static-method-inside-an-abstract-class-from-a-static-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter