java.lang.IllegalStateException:Action Bar Tab必须有一个Callback

时间:2012-08-07 13:46:27

标签: java android-actionbar illegalstateexception

我正在尝试向我的应用添加一个带有4个标签的ActionBarSherlock。 我试图从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

    }
}

2 个答案:

答案 0 :(得分:35)

您必须先在Tab上注册ActionBar.TabListener,然后再将其添加到actionBar。

在你的情况下:

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

否则我会建议你在他们自己的类中实现你的监听器,这样你的Activity就不会失去它的目的,而且也很容易丢失和可读。

答案 1 :(得分:0)

您可以重复此标签监听器

    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**