如何在android中获取getActionBar对象?
我试图在android中获取getActionBar对象。我获取null值为什么? 这是我的主java文件如何在android中获取getActionBar对象?,android,android-layout,android-intent,Android,Android Layout,Android Intent,我试图在android中获取getActionBar对象。我获取null值为什么? 这是我的主java文件 package com.example.naveen.tabfragment; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; public class MainActivity extends Activ
package com.example.naveen.tabfragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
public class MainActivity extends Activity {
// Declare Tab Variable
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar =getActionBar();
}
}
应用程序渐变文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.naveen.tabfragment"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.naveen.tabfragment" >
<uses-sdk
android:minSdkVersion="14"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
清单文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.naveen.tabfragment"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.naveen.tabfragment" >
<uses-sdk
android:minSdkVersion="14"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
style.xml文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light" >
<!-- Customize your theme here. -->
</style>
</resources>
我在这一行得到了空值
ActionBar ActionBar=getActionBar()强>
我正在使用android studio。并在MOTOE 4.4.2上进行测试,如果您使用的是支持库,则应使用 getSupportedActionBar()方法。 您还应该从支持的库而不是普通库导入适当的类。您的主题是AppCompat,因此扩展类必须是
AppCompatActivity
,而不是Activity
如果扩展AppcompatActivity类,则使用getSupportActionBar()
代替getActionBar()。
所以,最后
ActionBar ActionBar=getSupportActionBar()代码>
我希望它能帮助您。既然您使用
AppCompat
作为主题,那么您应该使用AppCompatActivity
而不是Activity
,使用getSupportActionBar()
而不是getActionBar()
。我试过了,上面出现了错误,请检查上面的..请检查我的更新版本我需要导入哪个类你可以导入android.support.v7.app.ActionBar
如果你不知道应该导入哪个类,简单的方法是使用word ActonBar删除导入,并允许Android Studio使用新导入提示您。记住选择一个有“支持”字样的。阅读谷歌上的参考资料。在页面顶部,您应该找到应该导入的类名。在这种情况下,正如Selim提到的,应该是:[android.support.v7.app.ActionBar]