Android: Implement inner class in outer class; cal

2019-08-26 01:28发布

I have multiple fragments in one activity that should be able to pass data inbetween them. I have used tutorials to implement callbacks. My MainActivity is the outer class in which my fragment classes are. Furthermore I have a FragmentPagerAdapter that handles the fragment transitions. Well the thing is, Eclipse wont let me implement my callback interface, that is included in one of the Fragments, for my MainActivity outer class.

This is the structure of my code:

public class MainActivity extends FragmentActivity **implements ConnectionFragment.dataCallback**{ 
//compiler error here:"ConnectionFragment cannot be resolved to a type" 
//when i leave this out i get runtime error: "MainActivity java must 
//implement dataCallback"

...

    public class SectionsPagerAdapter extends FragmentPagerAdapter  implements ConnectionFragment.dataCallback{
        @Override
        public void updateLog(View v, String line) {
            DataFragment dataFrag = (DataFragment)getSupportFragmentManager().findFragmentByTag(DataFragment.class.getName());
                if (dataFrag != null){
                    dataFrag.updateLog(v,line);
                }
        }
    ...

    }
    public static class ConnectionFragment extends Fragment {
    ...
        public interface dataCallback{
        public void updateLog(View v, String line);
        }
        dataCallback mCallback;
        private static dataCallback dummyCallback=new dataCallback(){
             @Override
             public void updateLog(View v, String line){    
             }
        };

        @Override
        public void onAttach(Activity activity){
            super.onAttach(activity);
            try {
                mCallback = (dataCallback)activity;
            } catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement dataCallback");
         }
    }
    }
    public static class DataFragment extends Fragment implements ConnectionFragment.dataCallback{
        public void updateLog(View v,String line){

             TextView logTextView=(TextView)v.findViewById(R.id.log_view);
             logTextView.append("\n"+line);
    }

    ...
    }
    public static class GraphFragment extends Fragment {
    ...
    }
}

ConnectionFragment should be able to send data to DataFragment.

I appreciate your help!

3条回答
2楼-- · 2019-08-26 01:41

Fragments should never directly interact with each other since this creates undesired coupling.

Your fragment should call the interface method implemented by the Activity to communicate with the Activity, then the Activity can communicate/relay that interaction with the other fragment.

查看更多
贼婆χ
3楼-- · 2019-08-26 01:43

You can't implement an inner-interface or extend an inner-class. Simply move ConnectionFragment to its own file.

This is because at compile time, these inner classes are dependent on the parent class - and NEVER the other way around. As proof, if you look at the compiled .class files, these inner-Objects are compiled as MainActivity$ConnectionFragment.class or something there-abouts. If, however, ConnectionFragment is compiled into its own file (ConnectionFragment.cass), then MainActivity.class can depend on it, and Eclipse will automatically handle the build-order.

查看更多
Animai°情兽
4楼-- · 2019-08-26 01:44

It's seem that you can't use an interface before you declare it.

查看更多
登录 后发表回答