我正在學習Android開發資料,現在正在學習添加ActionBar。android創建ActionBar
以下是DisplayMessageActivity.java的代碼,遇到錯誤「了java.lang.RuntimeException:無法啓動活動ComponentInfo」:
package com.mycompany.myfa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.*;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
//from SF
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
然而,當我註釋掉的代碼「getActionBar( ).setDisplayHomeAsUpEnabled(真)「; Apps可以成功運行,但沒有任何ActionBar。我錯過了什麼,所以「getActionBar()。setDisplayHomeAsUpEnabled(true);」失敗?
謝謝。我嘗試過使用getSupportActionBar()。setDisplayHomeAsUpEnabled(true);我嘗試使用getSupportActionBar()。setDisplayHomeAsUpEnabled(true);我嘗試使用getSupportActionBar()。setDisplayHomeAsUpEnabled(true);但錯誤仍然存在。以下是清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfa" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MyActivity"
android:theme="@android:style/Theme.Holo.Light" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfa.MyActivity" />
</activity>
</application>
</manifest>
再次感謝。
顯示您的清單文件 – Rajesh