narb
narb

Reputation: 988

swipe tabs and fragments behavior

I have a swipe tabs program that works fine.

I would like to pass data from the fragment to the containing activity. I've tried to follow (http://developer.android.com/training/basics/fragments/communicating.html)

I get an error on the following line:

    dataPasser = (onDataPass) activity;

with the following trace:

java.lang.ClassCastException: com.example.bernard.tab.MyActivity cannot be cast to com.example.bernard.tab.fragment1$onDataPass
        at com.example.bernard.tab.fragment1.onAttach(fragment1.java:34)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)

the code is attached:

public class fragment1 extends Fragment {

onDataPass dataPasser;

public fragment1() {
    // Required empty public constructor
}

public interface onDataPass {
    public void onPass(String s);
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    dataPasser = (onDataPass) activity;
}

Could you provide help in understanding this error?

A subsequent question:

my containing activity is as following:

public class MyActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private ActionBar actionBar;
private tabpager tabPagerAdapter;
private String[] tabs = { "A", "B", "C", "D", "E", "F" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    viewPager = (ViewPager) findViewById(R.id.pager);
    tabPagerAdapter = new tabpager(getSupportFragmentManager());
    viewPager.setAdapter(tabPagerAdapter);
    actionBar = getActionBar();
    etc ...

So my question is as it already has an "implements ActionBar.TabListener" how am I supposed to reference the interface onDataPass?

Some example code would be much appreciated :)

Thanks a lot in advance!

Upvotes: 2

Views: 161

Answers (1)

Viral Thakker
Viral Thakker

Reputation: 547

A class can extend only one super class but it can implements as many interface as it wants. Java class can have more than one interface . So just change this

public class MyActivity extends FragmentActivity implements
  ActionBar.TabListener,DataPass

Upvotes: 1

Related Questions