我绝对是Android的初学者。我即将创建带有标签的操作栏。但我的Android SDK版本太低了。所以我尝试使用ActionBarActivity创建带有标签的操作栏的旧方法。我也想知道新旧方式。现在我这样做。
我的活动课
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i = 1;i<=3;i++){
ActionBar.Tab tab = bar.newTab();
tab.setText("Tab" + i);
bar.addTab(tab);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
但是当我运行我的应用程序时。这是投掷错误。我的代码出了什么问题?
答案 0 :(得分:0)
不要使用ActionBarActivity
,因为它已被弃用。
你正在使用:
Theme.AppCompat.Light.DarkActionBar
with:ActionBarActivity
在java代码(AppCompatActivity
)中将其更改为Activity
。
这应该可以解决问题。当然,如果您使用AppCompat
:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmain);
setSupportActionBar(toolbar);
类似的问题: