0

_

Hello members of stackoverflow!

I worked successfully with the ViewPager (VP) on a "normal" activity.

Now I tried to use almost the same way for an Android Wear app.

In my MainActivity, which contains the VP, I faced different errors like:

  • Error 1: (getFragmentManager() -> "Cannot be applied"
  • Error 2: super(manager); -> "Cannot be applied"
  • Error 3: public Fragment getItem(int i); -> "Incompatible return type"

I found this thread and was able to fix those errors by changing the imports + changing the build.gradle.

How to implement view pager in Android watch?

Those are now my imports in my MainActivity.java:

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.wearable.activity.WearableActivity;
import java.util.ArrayList;
import java.util.List;

This is the rest of the MainActivity:

public class MainActivity extends WearableActivity
{
    ViewPager vp; // Variable for Viewpager

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Link to layout
        ViewPager vp = findViewById(R.id.vp); // Link variable to ID
        SetUpViewPager(vp); // Run viewpager method
    }
    public void SetUpViewPager(ViewPager viewpager)
    {
        MyViewPagerAdapter Adapter = new MyViewPagerAdapter(getFragmentManager());
        Adapter.AddPageFragment(new Fragment1(), "Fragment1");   // Load page 1 inside of ViewPager
        Adapter.AddPageFragment(new Fragment2(), "Fragment2");   // Load page 2 inside of ViewPager
        viewpager.setAdapter(Adapter);
    }
    public class MyViewPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
    {
        private List<Fragment> MyFragment = new ArrayList<>();
        private List<String> MyPageTitle = new ArrayList<>();

        public MyViewPagerAdapter(FragmentManager manager)
        {
            super(manager);
        }
        public void AddPageFragment(Fragment Frag, String Title)
        {
            MyFragment.add(Frag);
            MyPageTitle.add(Title);
        }
        @Override
        public Fragment getItem(int i)
        {
            return MyFragment.get(i);
        }
        @Nullable
        @Override
        public CharSequence getPageTitle(int position)
        {
            return MyPageTitle.get(position);
        }
        @Override
        public int getCount()
        {
            return 2; // 2 pages total
        }
    }
}

Strange is that is says ViewPager vp; -> Field 'vp' is never used!

Fragment1: (Fragment2 is the same except for 2/two)

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment
{
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View fragmentOne = inflater.inflate(R.layout.fragment1, container, false); // Link view and layout
        return fragmentOne;
    }
}

The XML files for the fragments are just constraint layouts with each 1 button. The content_main.xml contains the VP which accesses the activity_main.xml. I used the same way on the normal smartphone device and it worked flawlessly.

The problem is that everytime I start the app on the Wear Emulator, it immediately crashes.

This is what Logcat says:

10-23 14:40:32.909 2203-2203/com.example.user.wearvp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.wearvp, PID: 2203
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.wearvp/com.example.user.wearvp.MainActivity}: java.lang.NullPointerException: 
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: 
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.example.user.wearvp.MainActivity.SetUpViewPager(MainActivity.java:35)
at com.example.user.wearvp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5422) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Any help will be appreciated!

P.S:

If I forgot to mention anything important, please, just ask and I will give you the needed information!

Best regards

Stefan M.
  • 13
  • 4

1 Answers1

1

The stack trace indicates that there is no view with the id vp defined in your activity_main.xml layout file. I'm not completely sure what you mean with:

The content_main.xml contains the VP which accesses the activity_main.xml.

If vp is defined in the content_main.xml layout, then that might be the one you should use with setContentView() (instead of the current activity_main.xml).


Strange is that is says ViewPager vp; -> Field 'vp' is never used!

The local variable vp (defined inside the onCreate method) is shadowing the instance variable vp. If you need it to have object-level scope just remove ViewPager when you assign a value to it.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • Hi and thanks for your answer! It worked, but the strange thing is that I used the exact same in another app where it worked. For this reason I'm surprised it didn't work for this app... Probably I don't even need to use the content_main.xml but use just the activity_main.xml, but that's another topic. Thank you, TofferJ – Stefan M. Oct 26 '18 at 12:53