0

I'm pretty much a newbie when it comes to coding in android and I wonder how you can make a actionbar with swipe views.

My code: http://pastebin.com/iHZn27H3

Errors

Unknown entity 'ViewPager' on line 11
Unknown entity 'ViewPager' on line 21
Unknown type of field 'mViewPager' on line 21
Unknown type of field 'mViewPager' on line 22
Unknown method on line 22
Unknown entity 'ViewPager' on line 23
Unknown type of field 'mViewPager' on line 34
Unknown mrthod on line 34

How do I fix those errors?

EDIT: Errors now gone but I can't switch tab my swiping, only by clicking. Code: http://pastebin.com/iHZn27H3

TheMeisterSE
  • 541
  • 1
  • 7
  • 28
  • Check your API level you are building your code for. I think that you must have minimum API level 4 – Boris Mocialov Jun 26 '13 at 10:13
  • @mocialov-boris I might have misunderstood you but I checked AndroidManifest.xml and the min sdk version is 15, the target sdl version is 16 and the max sdk version is 17 – TheMeisterSE Jun 26 '13 at 10:27
  • Horizontal view paging is based upon APIs only available with the Android Compatibility package v4, Revision 3 ; these APIs are not available in the standard Android SDK at this time. Therefore, you will need to add the Android compatibility package to your Android project to access the appropriate APIs. Source: http://manishkpr.webheavens.com/android-viewpager-example/ For more here: http://developer.android.com/tools/extras/support-library.html#Downloading – Boris Mocialov Jun 26 '13 at 10:58
  • I looked into that but I already have Android-support-v4.jar in my libs folder. – TheMeisterSE Jun 26 '13 at 11:04

1 Answers1

1
  • Answer to the first sub-question:

I do not know which IDE you are using, but in Intellij you have to go to 'Module Settings' > 'Libraries' > click on plus sign > Java > libs (folder) > android-support-v4.jar. In Eclipse, probably, you have to go to 'Build Path' > 'Config Build Path' > 'Java Build Path' > 'Add JARs' > 'libs' > android-support-v4.jar


  • Answer to the second sub-question:

Extending http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/ as an example:

Tab1Fragment.java:

public class Tab1Fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.live, container,false);
        TextView tv = (TextView) view.findViewById(R.id.status);
        tv.setText("Fragment1");

        return view;
    }
}

Tab2Fragment.java:

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.live, container,false);
        TextView tv = (TextView) view.findViewById(R.id.status);
        tv.setText("Fragment2");

        return view;
    }
}

live.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:id="@+id/status"/>
</LinearLayout>

main activity (ViewPagerFragmentActivity.java):

public class ViewPagerFragmentActivity extends FragmentActivity {
    private PagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       super.setContentView(R.layout.activity_main);
       //initialsie the pager
       this.initialisePaging();
    }

    private void initialisePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }
}

main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <android.support.v4.view.ViewPager
    android:id="@+android:id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"></android.support.v4.view.ViewPager>

</LinearLayout>

PagerAdapter.java:

public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}
}
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • I am using "AIDE" at the moment (not at home so coding for android on my smartphone) I managed to remove the errors by adding the last few imports but now when I compile the app, I can swotch between tabs by clicking and not by swiping. Code: http://pastebin.com/iHZn27H3 – TheMeisterSE Jun 26 '13 at 12:13
  • How do I create a custom ViewPager? I'm so sorry but I don't really get it. Ain't the code above to notballow swiping between tabs? Due to the comments it seems like it will prevent so you can't swipe to switch between tabs? – TheMeisterSE Jun 26 '13 at 12:43
  • I am not sure about the second part of your request, make another SO question. I am deleting part of my answer – Boris Mocialov Jun 26 '13 at 12:58
  • @user2065518 try to follow this example: http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/ – Boris Mocialov Jun 26 '13 at 12:59
  • I really am doing something wrong. I followed the guide and it says there are no errors but when I start the app it freezes for a few seconds and then closes. – TheMeisterSE Jun 26 '13 at 13:48
  • @user2065518 works like a charm for me on emulator – Boris Mocialov Jun 26 '13 at 14:18
  • @user2065518 added some extra stuff to my answer which extends the example http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/ – Boris Mocialov Jun 26 '13 at 14:24
  • @user2065518 I think you should try with "normal" IDE – Boris Mocialov Jun 26 '13 at 14:25
  • @user2065518 here is another example: http://code.google.com/p/viewpagerexample/ – Boris Mocialov Jun 26 '13 at 14:25
  • This code is from Googles own guide for making tabs with swipeviews http://pastebin.com/MvQuwsmu this is my code based on Googles guide. Is it totally wrong? Could you maybe send some that works for you from top to bottom and then I'll check if I run into a error with my IDE? – TheMeisterSE Jun 26 '13 at 16:14
  • @user2065518 In my answer to your question, it is pretty much what works from top to bottom – Boris Mocialov Jun 27 '13 at 17:04
  • @user2065518 added main activity – Boris Mocialov Jun 27 '13 at 17:06