我正在创建一个像instagram这样的简单项目,它在主要活动中有3个标签。我想在tablayout选项卡中添加带有文本的图标,但它会抛出异常。
我的主要活动
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
}
private class CustomAdapter extends FragmentPagerAdapter {
private String items[] = {"Home", "Upload", "Profile"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new fragment1();
case 1:
return new fragment2();
case 2:
return new fragment3();
default:
return null;
}
}
@Override
public int getCount() {
return items.length;
}
@Override
public CharSequence getPageTitle(int position) {
return items[position];
}
}
}
此代码抛出nullpointer异常我该怎么办?
例外文字
09-29 14:33:53.004 29528-29528 / com.example.eyepatch.shutter E / AndroidRuntime:致命例外:主要 处理:com.example.eyepatch.shutter,PID:29528
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.eyepatch.shutter / com.example.eyepatch.shutter.MainActivity}: java.lang.NullPointerException:尝试调用虚方法 “android.support.design.widget.TabLayout $标签 android.support.design.widget.TabLayout $ Tab.setIcon(android.graphics.drawable.Drawable)” 在null对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 在android.app.ActivityThread.access $ 900(ActivityThread.java:157)
在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1356)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:148)
在android.app.ActivityThread.main(ActivityThread.java:5530)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:733) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.support.design.widget.TabLayout $ Tab android.support.design.widget.TabLayout $ Tab.setIcon(android.graphics.drawable.Drawable)” 在null对象引用上 在 com.example.eyepatch.shutter.MainActivity.onCreate(MainActivity.java:33) 在android.app.Activity.performCreate(Activity.java:6272)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 在android.app.ActivityThread.access $ 900(ActivityThread.java:157)
在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1356) 在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:148)
在android.app.ActivityThread.main(ActivityThread.java:5530)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:733) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
答案 0 :(得分:3)
您应首先将适配器设置为viewpager
重新安排以下几行:
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
如下所示:
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));
答案 1 :(得分:0)
结帐此链接。它对我帮助很大,我认为这就是你所需要的:
使用标签的Android Material Design
https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/