选项卡应用程序中的图标在启动spannable字符串时崩溃

时间:2017-03-21 02:21:31

标签: android tabs icons

我按照以下链接在标签布局中添加图标: https://guides.codepath.com/android/google-play-style-tabs-using-tablayout#sliding-tabs-layout

一切正常,包括带文字的标签。当我尝试添加图标时,应用程序会在启动时崩溃。我一直在寻找超过5小时,我对android很新。 : - (

我只在activity_main.xml中收到错误:

tools:context="com.example.android.viewpager.MainActivity"

在viewpager.MainActivity上显示带有注释的红色案例:

"Unresolved class, MainActivity --- Validates resources references inside android XML files." 

但是,当切换到仅文本选项卡时,此错误仍然存​​在并且应用程序在该情况下运行(错误仍然存​​在,因此我认为这不相关)。

我将更改隔离到下面的类,在文本和图标模式之间来回切换(用相应的标题消隐行)。这似乎不是样式的问题,是的,AllCaps在" TextAppearance.Design.tab"

中设置为false

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) 

0 个答案:

没有答案