Abdul Rehman
Abdul Rehman

Reputation: 21

"ActionBarSherlock" error after removing ActionBarSharelock from gradle implementation

Just recently I posted a question as I had a link issue. Then there I was told by a person that you cannot use ActionBarSharelock and AppCompat at the same time. So they advised me to remove the action bar and let AppCompat remain. Then I removed the ActionBarSharelock. And now this problem is coming. Now tell me what to do?

Some Screenshots and Code Here Below:

Screenshot:Problem 1

Screenshot:Problem 2

Screenshot:Problem 3

build.gradle (App)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.ummat.anmolmessages"
        minSdkVersion 14
        targetSdkVersion 29
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-ads:18.2.0'
}

MainActivity.java

package com.ummat.anmolmessages;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import android.view.KeyEvent;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener
{

    private String[] tabs = { "LATEST", "CATEGORY", "MY FAVORITES" };
    private TabsPagerAdapter mAdapter;
    private ViewPager viewPager;
    ActionBar.Tab tab;
    TextView txtapptitle;
    private AdView mAdView;
    private InterstitialAd mInterstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdView = (AdView) findViewById(R.id.adView);
        mAdView.loadAd(new AdRequest.Builder().build());

        mInterstitial = new InterstitialAd(this);
        mInterstitial.setAdUnitId(getResources().getString(R.string.admob_publisher_interstitial_id));
        mInterstitial.loadAd(new AdRequest.Builder().build());

        viewPager = (ViewPager) findViewById(R.id.pager);
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);

        //tab added in action bar
        for(String tab_name:tabs)
        {
            tab = getSupportActionBar().newTab();
            tab.setText(tab_name);
            tab.setTabListener(this);
            getSupportActionBar().addTab(tab);
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                getSupportActionBar().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 transaction) {
    }

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) 
        {
        case R.id.more_apps:

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Ummat"));
            startActivity(intent);
            return true;

        case R.id.menu_rateapp:

            final String appName = getPackageName();//your application package name i.e play store application url
            try {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("market://details?id="
                                + appName)));
            } catch (android.content.ActivityNotFoundException anfe) {
                startActivity(new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("http://play.google.com/store/apps/details?id="
                                + appName)));
            }
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub

            if (keyCode == KeyEvent.KEYCODE_BACK) {
                // Toast.makeText(appContext, "BAck", Toast.LENGTH_LONG).show();
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        MainActivity.this);
                alert.setTitle(getString(R.string.app_name));
                alert.setIcon(R.drawable.app_icon);
                alert.setMessage("Are You Sure You Want To Quit?");

                alert.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        //you may open Interstitial Ads here
                        if (mInterstitial.isLoaded()) {
                            mInterstitial.show();
                        }
                        finish();
                    }

                });

                alert.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub


                    }
                });
                alert.show();
                return true;
            }

            return super.onKeyDown(keyCode, event);

        }

    }

Upvotes: 0

Views: 96

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364351

You have to remove these import:

//import com.actionbarsherlock.app.ActionBar;
//import com.actionbarsherlock.app.SherlockFragmentActivity;
//import com.actionbarsherlock.app.ActionBar.Tab;
//import com.actionbarsherlock.view.Menu;
//import com.actionbarsherlock.view.MenuInflater;
//import com.actionbarsherlock.view.MenuItem;

Your Activity has to extend AppCompatActivity:

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
  //....
}

Also ActionBar.TabListener was deprecated. You have to consider to use a different component.

Upvotes: 1

Related Questions