John R
John R

Reputation: 2064

Android Tab Layout with Swipeable Views Having Errors

I am creating Android tab layout with swipeable views but I am getting some errors. I dont know how to solve these errors.

Code-

@SuppressLint("NewApi")
public class Types extends FragmentActivity implements ActionBar.TabListener{

    ImageView left, right;
    Intent objIntent;
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "type1", "type2", "type3", "type4", "type5", "type6", "type7", "type8" };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.types);
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
      }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(Types.this, PlayAudio.class);
        stopService(objIntent);
    }

    @Override 
    public void onBackPressed() {
        super.onBackPressed();
        objIntent = new Intent(Types.this, PlayAudio.class);
        stopService(objIntent); 
        startActivity(new Intent (Types.this, MainActivity.class));  
        finish();
    } 

    @Override 
    public void onResume()
    {
        super.onResume();
        objIntent = new Intent(Types.this, PlayAudio.class);
        startService(objIntent);
    }

}

Logcat-

01-29 01:45:36.094: E/AndroidRuntime(1191): Caused by: java.lang.NullPointerException
01-29 01:45:36.094: E/AndroidRuntime(1191): at com.info.abc.Types.onCreate(Types.java:38)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.Activity.performCreate(Activity.java:5243)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-29 01:45:36.094: E/AndroidRuntime(1191):     ... 11 more

This is line 38:

actionBar.setHomeButtonEnabled(false);

Import Files-

import android.support.v7.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

Upvotes: 0

Views: 224

Answers (2)

Spring Breaker
Spring Breaker

Reputation: 8251

By looking at your code, I am assuming that you are testing the app in a lower version (below API 11) device.Thats why you are getting the exception.ActionBar is introduced in API level 11. Now to make it work, you got 2 following ways,

1.Change your min SDK value to API 11.

2.Use android-support-v7-appcompat library which supports ActionBar from API level 7.

  • To use ActionBarCompat, your activity should extend ActionBarActivity.
  • change your activity theme to android:theme="@style/Theme.AppCompat"

For more information, visit the following site , http://android-developers.blogspot.in/2013/08/actionbarcompat-and-io-2013-app-source.html

Upvotes: 0

M D
M D

Reputation: 47817

If your activity does not have a title bar the method getActionBar() can return null.

You have two options to fix this:

  1. Add getWindow().requestFeature(Window.FEATURE_ACTION_BAR); before setContentView

  2. In your Manifest specify a Theme that supports ActionBar

Also make sure that you are targeting the right android version e.g. <uses-sdk android:minSdkVersion="11" />

Further Infromation go to this Link.

Upvotes: 1

Related Questions