gpt4 book ai didi

android - "Unfortunately, appname has stopped running"尝试使用 ActionBar.addTab 后

转载 作者:行者123 更新时间:2023-11-30 03:09:39 27 4
gpt4 key购买 nike

在浏览一些 Android 教程时,我一直在尝试将选项卡添加到操作栏,但每次我使用 addTab 时,都会出现问题。我的代码目前看起来像这样:

package com.example.myfirstapp;

import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.app.FragmentManager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

final ActionBar actionBar = getActionBar();

// actionBar.setSubtitle("mytest");
actionBar.setTitle("Title Goes Here");

// Set up the action bar to show tabs.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Here are a couple tabs
ActionBar.Tab PlayerTab = actionBar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionBar.newTab().setText("Fragment B");

// for each of the sections in the app, add a tab to the action bar.
actionBar.addTab(PlayerTab);
actionBar.addTab(StationsTab);

// Create a tab listener that is called when the user changes tabs.
// ActionBar.TabListener tabListener = new ActionBar.TabListener() {
// public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// // show the given tab
// }
//
// public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// // hide the given tab
// }
//
// public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// // probably ignore this event
// }
// };
//
// // Add 3 tabs, specifying the tab's text and TabListener
// for (int i = 0; i < 3; i++) {
// actionBar.addTab(
// actionBar.newTab()
// .setText("Tab " + (i + 1))
// .setTabListener(tabListener));
// }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// First run
// getMenuInflater().inflate(R.menu.main, menu);
// return true;

// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

public void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
public void openSettings() {
Toast.makeText(this, "Settings pressed", Toast.LENGTH_SHORT).show();

}
// Finding selected option item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
// menuItem = item;
// menuItem.setActionView(R.layout.progressbar);
// menuItem.expandActionView();
// TestTask task = new TestTask();
// task.execute("test");
// break;
return true;
case R.id.action_settings:
openSettings();

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

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

如果我用

注释掉这些行
    actionBar.addTab(PlayerTab);
actionBar.addTab(StationsTab);

代码工作正常(显示空白标签栏),但一旦我添加它们,我的应用程序就会崩溃。

最佳答案

根据 FragmentActivty 的文档:

If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

FragmentActivitygetActionBar() 适用于 API 11+。如果您的 minSDKVersion 是 11+,您应该使用 android.app.Activity 版本的 Activity 并获取 ActionBar< 的引用 就像你现在做的那样。如果您希望能够使用 FragmentsActionBar,请使用 ActionBarActivity,它是 v7 支持库的一部分

关于android - "Unfortunately, appname has stopped running"尝试使用 ActionBar.addTab 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com