【发布时间】:2014-12-25 05:52:44
【问题描述】:
在 android 设备 4.0.1 上,我正在尝试使用 ActionBar 构建应用程序,但得到了 NullPointException。我尝试了以下解决方案:
- 将主题
Theme.Holo.Light添加到应用程序主题中。 - 在活动的
OnCreate中,将setContentView之前的ActionBar功能设置为requestWindowFeature(Window.FEATURE_ACTION_BAR)。 - 使用
SherlockActivity并调用getSupportActionBar()
但没有运气。在所有方法中,我都得到了null ActionBar。谁能指出我的问题是什么。我在这里粘贴Activity 和AndroidManifest.xml。
AndroidManifest.xml
<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzoomers.birthdaysdiary"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light">
<activity
android:name="com.tzoomers.birthdaysdiary.BirthdaysDiary"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContentActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SyncActivity">
</activity>
</application>
</manifest>
</code>
SyncActivity.java
<code>
public class SyncActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.layout_sync_activity);
ActionBar actionBar = getActionBar();
if(actionBar != null)
{
getActionBar().setDisplayHomeAsUpEnabled(false);
}
else
{
Toast.makeText(this, "Action bar is null", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
请帮助解决问题,而不是指向urls。我已经尝试了所有解决方案。如果我在 XML 或 JAVA 文件中遗漏了某些内容,请指出。
提前致谢。
【问题讨论】:
-
应用程序主题应该类似于@style/Theme.Sherlock 并且活动需要扩展SherlockActivity
-
我试过了。将活动扩展到
SherlockActivity,并将主题添加到@style/Theme.Sherlock,但仍然在getSupportActionBar()上获得“NullPointException” -
另外你不需要“getWindow().requestFeature(Window.FEATURE_ACTION_BAR);”
-
是的,我知道没有必要使用
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);我刚刚给出了我遵循的步骤。使用的主题和要求的功能。我有printed value从getWindow().requestFeature(Window.FEATURE_ACTION_BAR);返回它给false。这可能是我不允许请求这些功能的问题吗?
标签: android android-actionbar android-actionbaractivity