Android interface

I always have this java.lang.IllegalStateException:Could not execute method of the activity problem. I planned to execute the event of the Android component (for example, the Button event - indicating the number of clicks of this button). Here is the code snippet for this problem:

 interface Selection { public void clicked(); } public class ParentClass extends FragmentActivity { // fTabs : FragmentTabHost // tabs : Map<String, Selection> private void initialize() { // fistFrag : FirstChildClass = new FirstChildClass() // secondFrag : SecondChildClass = new SecondChildClass() tabs.put("first", firstFrag); tabs.put("second", secondFrag); fTabs.add(fTab.newTabSpec("first").setTitle("First"), firstFrag.getClass(), null) fTabs.add(fTab.newTabSpec("second").setTitle("Second"), secondFrag.getClass(), null) } @Override public void onBackPressed() { tabs.get(fTabHost.getCurrentTabTag()).clicked(); } } public class FirstChildClass extends Fragment implements Selection { // data : TextView // hit : int = 0 @Override public void clicked() { data.setText(String.format("Hit Count: %d", ++hit)); } } public class SecondChildClass extends Fragment implements Selection { // data : TextView // hit : int = 0 @Override public void clicked() { data.setText(String.format("Hit Count: %d", ++hit)); } } 

I tried to provide a clicked() approach to the interaction between work views by calling a message on Logcat , and it worked, but when I used Button , the error above always tells me. I checked if data is null and it returned true . I am a little confused, I tried to check the invalidity of the data methods of the Activity , returns false , but when I access any method overrides by the interface, it always returns true . Is there any way to solve this?

+4
source share
1 answer

Here, my friend told me to solve this problem. Using getSupportFragmentManager . He also told me that creating an Activity or Fragment using its constructor is not applicable on the Android platform. So I switched to the usual way of adding tabs to FragmentTabHost .

 @Override public void onBackPressed() { //tabs.get(fTabHost.getCurrentTabTag()).clicked(); ((Selection) getSupportFragmentManager().findByFragmentByTag(fTabHost.getCurrentTabTag()).clicked(); } 
+1
source

Source: https://habr.com/ru/post/1493595/


All Articles