大家好我有这个问题。我实际上需要在android应用程序上添加两个选项卡,我正在阅读如何在developer.android.com中执行此操作,但我无法运行该程序
这是mi代码:
package com.example.example;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// 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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是编译错误:
02-23 16:09:20.164: E/AndroidRuntime(23928): FATAL EXCEPTION: main
02-23 16:09:20.164: E/AndroidRuntime(23928): java.lang.NoSuchMethodError: com.example.example.MainActivity.getActionBar
02-23 16:09:20.164: E/AndroidRuntime(23928): at com.example.example.MainActivity.onCreate(MainActivity.java:17)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1753)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.ActivityThread.access$1500(ActivityThread.java:155)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.os.Handler.dispatchMessage(Handler.java:130)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.os.Looper.loop(SourceFile:351)
02-23 16:09:20.164: E/AndroidRuntime(23928): at android.app.ActivityThread.main(ActivityThread.java:3820)
02-23 16:09:20.164: E/AndroidRuntime(23928): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 16:09:20.164: E/AndroidRuntime(23928): at java.lang.reflect.Method.invoke(Method.java:538)
02-23 16:09:20.164: E/AndroidRuntime(23928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
02-23 16:09:20.164: E/AndroidRuntime(23928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
02-23 16:09:20.164: E/AndroidRuntime(23928): at dalvik.system.NativeStart.main(Native Method)
我认为问题出在manifest.xml中,因为当我尝试插入这两行时:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
日食给我看了一个错误,上面写着“minium sdk应该是11而你的minium sdk是8”
然后我在manifest.xml中更改它,menssage错误消失,但我无法在手机上运行它
这里是mi android_manifest.mxl:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您应该使用support
库在运行SO版本低于API 11的设备中使用ActionBar
。
要使用ActionBar
,您应该致电:
final ActionBar actionBar = geSupportActionBar();
为确保一切正常,您必须import android.support.v7.app.ActionBar
。