I followed the link below to add icons in tab-layout: https://guides.codepath.com/android/google-play-style-tabs-using-tablayout#sliding-tabs-layout
and everything works fine, including the tabs with text. When I try to add the icons, the app crashes upon launch. I have been searching for over 5hrs and I'm pretty new to android. :-(
I get an error only in the activity_main.xml:
tools:context="com.example.android.viewpager.MainActivity"
Showing a red case on viewpager.MainActivity with comments:
"Unresolved class, MainActivity --- Validates resources references inside android XML files."
However, this error persists when switching to text only tabs AND the app runs in that case (with the error remaining, so I think this is not related).
I isolated the change to the following class, switching back and forth between text and icon mode (blanking out the lines with the respective titles). There doesn't seem to be an issue with the styles and yes, AllCaps is set to false in the "TextAppearance.Design.tab"
SimpleFragmentPagerAdapter.java:
package com.example.android.miwok;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
/**
* Provides the appropriate {@link Fragment} for a view pager.
*/
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
// if you work with text in the tabs
private String tabTitles[] = new String [] { "Time", "$", "Company", "A trip" };
// Always needed
private Context context;
//if you work with Icons in the tabs
/*
private int[] imageResId = {
R.drawable.ic_access_time_white_24dp,
R.drawable.ic_attach_money_white_24dp,
R.drawable.ic_local_bar_white_24dp,
R.drawable.ic_directions_car_white_24dp
};
*/
public SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new NumbersFragment();
} else if (position == 1){
return new FamilyFragment();
} else if (position == 2){
return new ColorsFragment();
} else {
return new PhrasesFragment();
}
}
@Override
// Generate title based on item position
public CharSequence getPageTitle(int position) {
//If you work with text in the tabs
return tabTitles[position];
//If you work with icons in the tabs
/*
Drawable image = context.getResources().getDrawable(imageResId[position]);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
*/
}
}
MainActivity:
package com.example.android.miwok;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.content.Intent;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
/**
* Displays a {@link ViewPager} where each page shows a different page.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
if (viewPager != null) {
viewPager.setAdapter(adapter);
}
// Give the Tab layout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
if (tabLayout != null) {
tabLayout.setupWithViewPager(viewPager);
}
}
}
Logcat:
03-21 13:53:00.450 1971-1971/? I/art: Late-enabling -Xcheck:jni
03-21 13:53:00.470 1971-1978/? E/art: Failed sending reply to debugger: Broken pipe
03-21 13:53:00.470 1971-1978/? I/art: Debugger is no longer active
03-21 13:53:00.470 1971-1978/? I/art: Starting a blocking GC Instrumentation
03-21 13:53:00.582 1971-1971/? W/System: ClassLoader referenced unknown path: /data/app/com.example.android.miwok-1/lib/arm64
03-21 13:53:00.591 1971-1971/? I/InstantRun: Starting Instant Run Server for com.example.android.miwok
03-21 13:53:01.128 1971-1971/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-21 13:53:01.222 1971-1971/? D/AndroidRuntime: Shutting down VM
03-21 13:53:01.223 1971-1971/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.miwok, PID: 1971
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6217)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.example.android.miwok.SimpleFragmentPagerAdapter.getPageTitle(SimpleFragmentPagerAdapter.java:69)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:773)
at android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:764)
at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:716)
at com.example.android.miwok.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6217)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)