Android- ActionBar AutoCompleteTextview

时间:2015-01-30 06:36:48

标签: java android xml android-actionbar menuitem

我在操作栏中添加了一个AutoCompleteTextview,方法是在菜单文件夹中向xml添加项目并对其进行充气。但是我无法为我的字符串[]设置适配器,因为引用AutoCompleteTextview导致第35行出错,即。

AutoCompleteTextView et=(AutoCompleteTextView)menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.edt);

如何解决此问题并使其在我的操作栏中正常工作。

菜单> main.xml中

<item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:actionLayout="@layout/search_bar"/>

search_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<AutoCompleteTextView 
    android:id="@+id/edt"
    android:layout_width="50dp"
    android:layout_height="wrap_content"/>

</LinearLayout>

mainactivity.java

public class MainActivity extends Activity {

String [] products={"candle stick 1","candlestick 3","sglc10","senson","lawn"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00ffff")));
    actionBar.setSubtitle("mytest");
    actionBar.setTitle("welcome"); 

//      AutoCompleteTextView et=(AutoCompleteTextView)menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.edt);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, products);
//      et.setAdapter(adapter);

    return true;
}

logcat的

01-30 01:13:18.309: E/AndroidRuntime(1804): FATAL EXCEPTION: main
01-30 01:13:18.309: E/AndroidRuntime(1804): Process: com.example.action_bar, PID: 1804
01-30 01:13:18.309: E/AndroidRuntime(1804): java.lang.NullPointerException
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.example.action_bar.MainActivity.onCreateOptionsMenu(MainActivity.java:35)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.app.Activity.onCreatePanelMenu(Activity.java:2537)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.view.Choreographer.doFrame(Choreographer.java:531)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.os.Handler.handleCallback(Handler.java:733)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.os.Looper.loop(Looper.java:137)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at java.lang.reflect.Method.invoke(Method.java:515)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-30 01:13:18.309: E/AndroidRuntime(1804):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

尝试以下代码: -

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /** Create an option menu from res/menu/items.xml */
    getMenuInflater().inflate(R.menu.items, menu);

    /** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id.search).getActionView();

    /** Get the edit text from the action view */
    EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);

    /** Setting an action listener */
    txtSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    return super.onCreateOptionsMenu(menu);
}

了解更多信息,请参阅以下链接: -

http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/