Note that there are some explanatory texts on larger screens.

plurals
  1. POonSomeEventListener crash
    primarykey
    data
    text
    <p>I have set up an application with a main activity and 2 fragment activities. I use a onsomeevent to pass data from the fragment to the main activity. The first fragment works fine and passes data. There is only one fragment running at a time. I copied the same event listener to second fragment but it crashes with "" must implement onSomeEventListener". I am stuck.</p> <p>the main activity class I implement the listener</p> <pre><code> public class SimpleRGB_Main extends FragmentActivity implements onSomeEventListener { </code></pre> <p>I also capture the data passed from the first fragment slider controls</p> <pre><code> // Return event for RGB controls. @Override public void someEvent(int FixtureNumber, int R_value, int G_value, int B_value) { } </code></pre> <p>The first fragment is called slider controls</p> <pre><code> public class SliderControls extends Fragment { public interface onSomeEventListener { public void someEvent(int position, int R_Value, int G_value, int B_value); } onSomeEventListener someEventListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { someEventListener = (onSomeEventListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); } } </code></pre> <p>Second fragment</p> <pre><code> public class ButtonControls extends Fragment { public interface onSomeEventListener { public void someEvent(int position, int R_Value, int G_value, int B_value); } onSomeEventListener someEventListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { someEventListener = (onSomeEventListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); } } </code></pre> <p>Here are the changed I made. I separated the listener and renamed them to make them easy to follow. In main activity. </p> <pre><code> // Return event for RGB controls. @Override public void onFragmentSliderDoSomething(int FixtureNumber, int R_value, int G_value, int B_value) { public void onFragmentButtonsDoSomething(int FixtureNumber, int R_value, int G_value, int B_value) { </code></pre> <p>Also in the main activity be sure to implement both listeners</p> <pre><code> public class SimpleRGB_Main extends FragmentActivity implements SliderControls.FragmentAListener, ButtonControls.FragmentBListener { </code></pre> <p>In Button Fragment</p> <pre><code> public class ButtonControls extends Fragment { private FragmentBListener someEventListener; public interface FragmentBListener { public void onFragmentButtonsDoSomething(int position, int R_Value, int G_value, int B_value); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { someEventListener = (FragmentBListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); } } </code></pre> <p>In Slider Fragment</p> <pre><code> public class SliderControls extends Fragment { private FragmentAListener someEventListener; public interface FragmentAListener { public void onFragmentSliderDoSomething(int position, int R_Value, int G_value, int B_value); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { someEventListener = (FragmentAListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload