我有一个简单的东西AppCompatActivity,一个简单的东西Toolbar(android.support.v7.widget.Toolbar),一个设计库TabLayout和一个ViewPager。
现在,在这种情况下,我有4个片段,它们位于viewpager内部,可以从侧面滑动。很好 但是,一旦我使用mTabLayout.setupWithViewPager(mViewPager);,图标就会消失,但是我可以在空白处进行切换,效果很好。如果我删除该行,图标将再次可见,“滑动”将起作用,但是如果我使用Tab键选择图标,则不会发生任何事情。
我的密码
主要活动
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ViewPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
initTabLayout();
initViewPager();
mTabLayout.setupWithViewPager(mViewPager);//Here is the code from above
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
private void initTabLayout() {
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_fire_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_apps_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_account_plus_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_help_white));
}
private void initViewPager() …Run Code Online (Sandbox Code Playgroud) 我是"Android-App-Dev"-Scene的新手,有一个问题:
如何为我的应用轻松制作一个干净整洁的设置页面?
有一些标题和某些大按钮可以选项卡转到新页面.
我正在使用Android Studio并知道如何创建新页面,类等.
当我测试我的应用程序时,我在 Android-Studio-Consol 中收到以下错误:
08-21 13:56:28.059 9637-9637/net.dominik.genpush E/AndroidRuntime? FATAL EXCEPTION: main
Process: net.dominik.genpush, PID: 9637
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.dominik.genpush/net.dominik.genpush.settings}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at net.dominik.genpush.settings.<init>(settings.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515) …Run Code Online (Sandbox Code Playgroud)