I am developing an Android app. In my app I need to work with tabs. But when I add tabs to TabLayout in code, it is throwing error. What is wrong with my code?
This is my tablayout XML in custom action bar:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/action_bar_padding_top"
android:paddingBottom="@dimen/action_bar_padding_bottom"
android:paddingLeft="@dimen/action_bar_padding_left"
android:paddingRight="@dimen/action_bar_padding_right">
<ImageButton
android:id="@+id/open_left_drawer_icon"
android:background="@drawable/nav_open"
android:layout_width="@dimen/nav_icon_width"
android:layout_height="@dimen/nav_icon_height" />
<TextView
android:layout_centerHorizontal="true"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/main_logo_text_size"/>
<ImageButton
android:layout_alignParentRight="true"
android:background="@drawable/wishlist_icon"
android:layout_width="@dimen/nav_icon_width"
android:layout_height="@dimen/nav_icon_height" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_tab_layout"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize" />
</LinearLayout> </LinearLayout>
How I set up tabs to tablayout in activity:
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout)findViewById(R.id.main_tab_layout);
setupTabs();
}
private void setupTabs() {
tabLayout.addTab(tabLayout.newTab().setTag(1).setText("TRENDY/HOT"));
tabLayout.addTab(tabLayout.newTab().setTag(2).setText("MEN"));
tabLayout.addTab(tabLayout.newTab().setTag(3).setText("WOMEN"));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
break;
case 2:
break;
default:
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
break;
case 2:
break;
default:
break;
}
}
});
}
}
This is the error in logacat when I run the app:
03-10 12:52:12.610 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: VFY: unable to resolve virtual method 496: Landroid/content/res/TypedArray;.getType (I)I
03-10 12:52:12.610 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-10 12:52:12.618 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_FOR_ALLOC freed 143K, 10% free 2617K/2892K, paused 3ms, total 4ms
03-10 12:52:12.670 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_FOR_ALLOC freed 21K, 10% free 2870K/3168K, paused 1ms, total 2ms
03-10 12:52:12.706 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_FOR_ALLOC freed 98K, 12% free 2992K/3392K, paused 3ms, total 3ms
03-10 12:52:12.710 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion I/dalvikvm-heap: Grow heap (frag case) to 4.121MB for 1127532-byte allocation
03-10 12:52:12.718 7428-7430/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_CONCURRENT freed <1K, 9% free 4093K/4496K, paused 2ms+1ms, total 7ms
03-10 12:52:12.722 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_FOR_ALLOC freed <1K, 9% free 4094K/4496K, paused 2ms, total 2ms
03-10 12:52:12.726 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion I/dalvikvm-heap: Grow heap (frag case) to 6.541MB for 2536932-byte allocation
03-10 12:52:12.734 7428-7430/com.blog.waiyanhein.mmfashion.mmfashion D/dalvikvm: GC_CONCURRENT freed 0K, 6% free 6571K/6976K, paused 1ms+1ms, total 4ms
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion D/AndroidRuntime: Shutting down VM
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa61d4908)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blog.waiyanhein.mmfashion.mmfashion/com.blog.waiyanhein.mmfashion.mmfashion.MainActivity}: java.lang.NullPointerException
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: Caused by: java.lang.NullPointerException
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.MainActivity.setupTabs(MainActivity.java:395)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.MainActivity.onCreate(MainActivity.java:98)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 12:52:12.774 7428-7428/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
03-10 12:52:12.778 396-407/system_process W/ActivityManager: Force finishing activity com.blog.waiyanhein.mmfashion.mmfashion/.MainActivity
03-10 12:52:12.898 396-407/system_process D/dalvikvm: GC_FOR_ALLOC freed 175K, 35% free 6244K/9560K, paused 16ms, total 16ms
03-10 12:52:12.898 396-407/system_process I/dalvikvm-heap: Grow heap (frag case) to 7.042MB for 856092-byte allocation
03-10 12:52:12.910 396-411/system_process D/dalvikvm: GC_FOR_ALLOC freed 4K, 32% free 7075K/10400K, paused 12ms, total 12ms
03-10 12:52:12.938 396-399/system_process D/dalvikvm: GC_CONCURRENT freed 7K, 31% free 7191K/10400K, paused 9ms+1ms, total 28ms
03-10 12:52:12.938 396-412/system_process D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 4ms
Why cannot set tabs to TabLayout? But when I remove calling setUpTabs function in onCreate, app is running fine.
Your id of TabLayout in your xml is @+id/tab_layout"
but you're finding the view with id main_tab_layout
in your Activity.
Line tabLayout = (TabLayout)findViewById(R.id.main_tab_layout);
It should be tabLayout = (TabLayout)findViewById(R.id.tab_layout);