2017-08-31 59 views
-1

错误调用我的代码:如何解决表格布局上的方法调用错误?

Method invocation 'getIcon' may produce 'java.lang.NullPointerException' less... (Ctrl+F1).This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report NullPointerException (NPE) errors that might be produced.More complex contracts can be defined using @Contract annotation, for example:@Contract(", null -> null") — method. returns null if its second argument is null @Contract(", null -> null; _, !null -> !null")— method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it.The inspection can be configured to use custom @[email protected] annotations(by default the ones from annotations.jar will be used).

另一个错误:

setColorFilter(int,android.graphics.PorterDuff.Mode)' is deprecated less... (Ctrl+F1).This inspection reports where deprecated code is used in the specified inspection scope. Method invocation setColorFilter' may produce java.lang.NullPointerException' less... (Ctrl+F1) .

我Viewpager代码:

public class PagerAdapterStudent extends FragmentStatePagerAdapter{ 
int mNumOfTabs; 
public PagerAdapterStudent(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
    } 
    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return new PersonalFragment(); 
      case 1: 
       return new AddressFragment(); 
      case 2: 
       return new GuardianFragment(); 
      default: 
       return null; 
} 
    } 
@Override 
    public int getCount() { 
     return mNumOfTabs; 
    } 
} 

我的活动代码:

public class Profile extends AppCompatActivity { 
Progressbar progressbar; 
public String tag; 
    protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.profile); 
     progressbar= new Progressbar(this); 
     final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     final ViewPager viewPager =(ViewPager)findViewById(R.id.pager); 
     final PagerAdapterStudent adapter = new PagerAdapterStudent(getSupportFragmentManager(),tabLayout.getTabCount()); 
     viewPager.setAdapter(adapter); 
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_profile).setText("Personal")); 
     tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_address).setText("Address")); 
     tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_guardian).setText("Guardian")); 
     tabLayout.getTabAt(0).getIcon().clearColorFilter(); 
     tabLayout.getTabAt(1).getIcon().clearColorFilter(); 
     tabLayout.getTabAt(2).getIcon().clearColorFilter(); 
     int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.Profile_color); 
tabLayout.getTabAt(0).getIcon().setColorFilter(tabIconColor,PorterDuff.Mode.SRC_IN); 
     tabLayout.setTabTextColors(Color.BLACK, tabIconColor); 
     tabLayout.setTabGravity(TabLayout.MODE_SCROLLABLE); 
     Intent i = getIntent(); 
     int tabToOpen = i.getIntExtra("FirstTab", -1); 
     if (tabToOpen!=-1) { 
       tabLayout.getTabAt(0).getIcon().clearColorFilter(); 
      tabLayout.getTabAt(2).getIcon().setColorFilter(tabIconColor,PorterDuff.Mode.SRC_IN); 
      viewPager.setCurrentItem(2, true); 
      } 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
       int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.Profile_color); 
       tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); 
       tabLayout.setTabTextColors(Color.BLACK, tabIconColor); 
       tabLayout.setSelectedTabIndicatorColor(tabIconColor); 
      }@Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
       int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.Profile_color); 
//    tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); 
       tab.getIcon().setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN); 
      tabLayout.setSelectedTabIndicatorColor(tabIconColor); 
       tabLayout.setTabTextColors(Color.BLACK, Color.BLACK); 
      } 
      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 
      } 
     }); 
} 
} 

I H大家阅读了与同样问题有关的其他问题,但没有任何帮助。请帮帮我。

+0

检查此链接[android-material-design-working-with-tabs](https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/) –

回答

0
TabLayout.Tab tab= tabLayout.getTabAt(0); 
     if(tab !=null) { 
      if(tab.getIcon()!=null) { 
       final int tabIconColor = ContextCompat.getColor(getBaseContext(), R.color.helpdesk_color); 
       tab.getIcon().clearColorFilter(); 
       tab.getIcon().setColorFilter(tabIconColor,PorterDuff.Mode.SRC_IN); 
      } 
     } 
相关问题