/ / Android getSupportActionbar () ma wartość Null, gdy Activity jest wywoływana z tabhost - android, android-layout, actionbarsherlock

Android getSupportActionbar () ma wartość Null, gdy wywoływana jest Activity z tabhost - android, układ androida, actionbarsherlock

Mam pasek akcji wsparcia, który działa dobrze, jeśli jawywołać zdarzenie z zamiarem ręcznie. Ale jeśli pozostawię to tabhostowi, aby go wywołać, wtedy pasek akcji zwrócony z getSupportActionbar () ma wartość null.

Słyszałem, że w Stack wspomniano o tym w innym pytaniu, ale nikt nie udzielił odpowiedzi i nie odpowiedział (najwyraźniej występuje tylko na Androidzie 3 i nowszych). Czy ktoś ma jakieś pomysły?

Mój tabhost:

public class NavTab extends TabActivity {

TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

// Initialize a TabSpec for each tab and add it to the TabHost
intent = new Intent().setClass(this, SummaryPage.class);
spec = tabHost.newTabSpec("Summary");
spec.setIndicator("Account", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);

//Feedback
intent = new Intent().setClass(this, FeedbackPage.class);
spec = tabHost.newTabSpec("Feedback");
spec.setIndicator("Feedback", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);

//Payment Locations
intent = new Intent().setClass(this, PaymentLocationsActivity.class);
spec = tabHost.newTabSpec("Payment Locations");
spec.setIndicator("Pay Loc", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);

//Usage Alert
intent = new Intent().setClass(this, UsageAlertPage.class);
spec = tabHost.newTabSpec("Usage Alerts");
spec.setIndicator("Alerts", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(1);

}


}

Moja aktywność

public class PageWithActionBar extends SherlockActivity implements ActionBar.OnNavigationListener {

private static String TAG = "mymeter-Main";
private List<Account> accounts = new LinkedList<Account>();
private LocationAdapter locationAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);


ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowTitleEnabled(false);

accounts.add(new Account("123456789", "4-15 Rose Rd", "Auckland 1021"));
accounts.add(new Account("0987654321", "49 Ronaki Rd", "Auckland 1043"));
locationAdapter = new LocationAdapter(this, accounts);
actionBar.setListNavigationCallbacks(locationAdapter, this);


}
}

Odpowiedzi:

2 dla odpowiedzi № 1

Twoja TabActivity musi się rozszerzyć SherlockActivity, sprawdź przykłady ActionBarSherlock na kartach