java.lang.IllegalStateException:操作栏标签必须有一个回调(java.

2019-07-30 19:56发布

我想添加一个ActionBarSherlock有4个选项卡,以我的应用程序。 我试图从SherlockActivity延长then SherlockFragmentActivity`,但我仍然得到这个异常:

java.lang.IllegalStateException:操作栏标签必须有一个回调。

    import java.io.IOException;
    import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
import org.json.JSONObject;

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

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends SherlockFragmentActivity implements OnClickListener, OnItemClickListener, TabListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        try{

            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            Tab homeTab = actionBar.newTab();
            homeTab.setIcon(R.drawable.tab_home);
            Tab exploreTab = actionBar.newTab();
            exploreTab.setIcon(R.drawable.tab_explore);
            Tab leaderBoardTab = actionBar.newTab();
            leaderBoardTab.setIcon(R.drawable.tab_leaderboard);
            Tab profileTab = actionBar.newTab();
            profileTab.setIcon(R.drawable.tab_profile);

            actionBar.addTab(homeTab);
            actionBar.addTab(exploreTab);
            actionBar.addTab(leaderBoardTab);
            actionBar.addTab(profileTab);
                     }catch(Exception ex){

            Log.e("error from onCreate" , ex.toString());
            Log.e("error from onCreate" , ex.getStackTrace().toString());
            }

               }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }



    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }



    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

Answer 1:

将它添加到动作条之前,必须先注册一个标签的ActionBar.TabListener。

你的情况:

Tab homeTab = actionBar.newTab();
homeTab.setTabListener(this) //'this' because your activity implements a TabListener
...
actionBar.addTab(homeTab);

否则,我会建议你实现你的听众在自己的班级,这样你的活动不失去其目的,也容易understendable和可读性。



Answer 2:

可以重复选项卡监听器,因为这

    ActionBar.Tab tab1 = actionBar.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2 = actionBar.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);

    ActionBar.Tab tab3 = actionBar.newTab();
    tab3.setText("Tab 3");
    tab2.setTabListener(this); **// this is must be tab3 not tab2**


文章来源: java.lang.IllegalStateException: Action Bar Tab must have a Callback