2014-09-23 44 views
1

我從以下PDF Docant書官方Android教程。出於某種原因,我的搜索圖標不會顯示出來。我包括來自holo_dark的ic_action_search.png照片,以及使用全暗光與黑暗動作欄主題的即時通訊。 下面是代碼,沒有被蝕發現錯誤回報,最小SDK是設置爲11在操作欄搜索圖標不會出現

DisplayMessageActivity.java文件

package com.example.myfirstapp1; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class DisplayMessageActivity extends ActionBarActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
// Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.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); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu items for use in the action bar 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.main_activity_actions, menu); 
return super.onCreateOptionsMenu(menu); 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 
} 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<!-- Search, should appear as action button --> 
<item android:id="@+id/action_search" 
android:icon="@drawable/ic_action_search" 
android:title="@string/action_search" 
android:showAsAction="always" /> 
<!-- Settings, should always be in the overflow --> 
<item android:id="@+id/action_settings" 
android:title="@string/action_settings" 
android:showAsAction="never" /> 
</menu> 

回答

0

,我認爲你的錯誤是在你的onCreate method,在這一行:

setContentView(textView);

您應該將內容視圖設置爲您的視圖的XML,而不是一個TextView。

你的onCreate應該是這個樣子:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(the_name_of_your_xml_for_this_activity); 
    // Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
    // Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 
} 
+0

感謝,但,當我把這個XML活動錯誤activity_display_message的我的名字不能被解析爲一個變量出現 – brumbrum 2014-09-23 19:42:58

+0

嘗試setContentView(R.layout.activity_display_message) – 2014-09-23 19:45:24

+0

我認爲我們正在取得進展!謝謝!它將@ string/action_search後面的標題放入了溢出。我的操作欄中仍然沒有ic_action_search圖標。 – brumbrum 2014-09-23 20:00:25

0

我認爲你需要先定義它RES/valuse/drawables.xml

<item name="ic_action_search" type="drawable">@android:drawable/ic_menu_search</item> 

檢查照片,

enter image description here

如果你有一個.bng照片你必須通過另一種方式來將其導入以繪製不是使用

android:src="@drawable/ic_action_search"