2015-10-11 52 views
0

我正在學習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> 

再次感謝。

+0

顯示您的清單文件 – Rajesh

回答

0

可能是其主題的問題,您使用的是不支持的行動bar..try改變你的清單文件......

android:theme="@android:style/Theme.Holo.Light" 

如果它不工作,然後嘗試做到這一點

getSupportActionBar() or ((ActionBarActivity)getActivity()).getSupportActionBar(). 

希望工程..

0

您是從AppCompatActivity繼承。因此,你需要使用getSupportActionBar(),而是getActionBar(),拿到程序兼容性-V7支持的操作欄

還需要在你的style.xml主題添加此XML標記。

<item name="android:windowActionBar">true</item>