2017-08-31 75 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); 
      } 
     } 
相關問題