0

OK. I have seen many questions along these lines, but it's possible that my issue is so basic (i.e. stupid n00b question), that no one has bothered to ask.

I'm still very new to Android programming, or using Java in an "industrial" capacity (i.e. not for simple 10-line classroom problems).

I have an activity class with an embedded static class:

package com.example.android.effectivenavigation;
•
•
•
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    •
    •
    •
    public static class NonSwipeableViewPager extends ViewPager {
        •
        •
        •
    }
}

This comes from a sample from this site. What I am trying to do, is reduce it to the most basic, atomic elements.

Specifically, I want to have the entire example in the main activity file.

Here is my (non-funtional) XML:

<com.example.android.effectivenavigation.MainActivity.NonSwipeableViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Now, the problem is that when I specify the ViewPager in the layout XML, I get a "ClassNotFoundException" when it comes time to unwrap the NonSwipeableViewPager element.

It works fine if I set the NonSwipeableViewPager up in a separate file and reference it fairly directly. It's when it's embedded as a static class into the main activity class that I have issues.

I'm obviously using the wrong namespace in the XML.

Can anyone help me to find the correct namespace to use?

Thanks!

Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
  • Make it a singe file. – Thomas R. Aug 17 '15 at 13:48
  • Besides being a duplicate on StackOverflow, it's a bad programming approach to mix the presentation layer (Activity) with the view layer (NonSwipeableViewPager) the way you're doing. – Budius Aug 17 '15 at 13:57
  • @Budius Yes, I know. However, this is not something that I want to actually use. I'm learning, and the original example had a lot of extra "cruft" that I'm getting rid of in order to focus on just the part I want. I'm actually a very good programmer (30+ years of experience). I'm rather familiar with the concept of encapsulation. The code that I'll write once I get this down will be quite good. – Chris Marshall Aug 17 '15 at 14:37
  • It was just a suggestion. I'm happy you understand the importance of proper encapsulation. But still, check the linked question, it shows how to point to inner class. – Budius Aug 17 '15 at 14:38
  • @Budius Thanks for pointing out the duplicate. The issue I'm having with learning Android is that the SNR of Android data is awful. Pretty much 1.0. I get so many responses to my queries that it's almost impossible to sort it out. I'm sure that, as I improve, I'll get better at it, but I ain't there yet. – Chris Marshall Aug 17 '15 at 14:39
  • Yeeaaahhh... None of this stuff is working. I get casting issues if I try the superclass, and I can't seem to talk it out of flipping out. This fox ain't worth the chase, I'll just revert to a separate class file. As @Budius mentioned, that's the proper thing to do anyway. This will all get thrown away by this afternoon, so it ain't worth fretting over. Thanks, folks. – Chris Marshall Aug 17 '15 at 14:53

1 Answers1

1

Try to have something like this in your main activity's xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                               xmlns:tools="http://schemas.android.com/tools"
                               android:id="@+id/pager"
                               android:layout_width="match_parent"
                               android:layout_height="match_parent"
                               tools:context="com.example.android.effectivenavigation.MainActivity.NonSwipeableViewPager"/>

Hope this helps.

Edit: I'm still new user and I am not used yet to how extensive my answers should be. So here is a bit more of an example of what I did:

You can extent Fragment in your custom class and have an onCreateView method in it. Also you should have a separate layout xml (in my example named fragment_example) for this fragment. The method should return a View object that inflates fragment's layout. Here is an example:

public static class ExampleFragment extends Fragment {

    static Button b;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_exmaple, container, false);

        b = (Button) rootView.findViewById(R.id.button1);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something
            }
        });

        return rootView;
    }

}

And here is the corresponding layout xml:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          tools:context=".activity_name">

          <Button
            android:layout_width="227dp"
            android:layout_height="40dp"
            android:text="Search"
            android:id="@+id/search"
            android:layout_gravity="center_horizontal"
            android:background="#80ffffff"
            android:textColor="#ffff6b6b"/>

 </LinearLayout>

You will also need an FragmentPagerAdapter class to create the fragments:

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {

            default:
                return new ExampleFragment();
        }
    }

    @Override
    public int getCount() {
        return 1;
    }
}

Finaly in your activitys onCreate method you should have something like that:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the two primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);

}

Heavily based in this: http://developer.android.com/training/implementing-navigation/lateral.html

I hope this offers more detail and helps you more

jrsall92
  • 572
  • 1
  • 5
  • 19
  • Cool. Just point to the superclass. I appreciate the suggestion. – Chris Marshall Aug 17 '15 at 14:40
  • This will not create/inflate the custom class. This will inflate the super class. – Budius Aug 17 '15 at 14:48
  • @Budius I expanded my answer I hope this offers better quality. – jrsall92 Aug 17 '15 at 16:30
  • 1
    hi @jrsall92 it's not about new user or how to create the fragments inside the ViewPager. The issue is that using `android.support.v4.view.ViewPager` in your XML layout with a `tools:context` pointing to the custom class will not inflate the custom class. It will still inflate the `android.support.v4.view.ViewPager`. On the question/answer that I marked this is duplicate shows the correct way of pointing an XML layout to a custom inner class. That is `` – Budius Aug 17 '15 at 16:47
  • @Budius I don't argue that you are pointing to a better solution, just saying that since he is a fresh android developer (as am I) and tries to create a "consumer", as in more that a few lines, app (as am I), I find it better to use an example given by Android's page on the matter. Yet again I will surely checkout the duplicate as a way to learn better programming. – jrsall92 Aug 17 '15 at 18:11