在简单布局中添加SearchView

时间:2016-01-30 08:14:58

标签: android searchview

我正在尝试在相对布局中添加搜索视图,但它提供了一个空异常。任何帮助将不胜感激。我刚刚在相对布局中添加了SearchView小部件,基本上,我想添加我在searchView中创建的可搜索信息,这是我的代码 - >

SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView=(SearchView) findViewById(R.id.searchbox);
    searchView.setFocusable(true);// searchView is null 
    searchView.setFocusableInTouchMode(true);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

这是SearchView的XML代码

<RelativeLayout
        android:layout_margin="10dp"
        android:id="@+id/search_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:tag="sticky"
        android:background="#ffffff"
        >

         <SearchView
             android:id="@+id/searchbox"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            ></SearchView>
    </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

我使用了你的代码,并且在我的设备上运行正常,可以与之进行比较。

activity_main.xml

  <RelativeLayout
        android:layout_margin="10dp"
        android:id="@+id/search_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:tag="sticky"
        android:background="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <SearchView
            android:id="@+id/searchbox"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            ></SearchView>
    </RelativeLayout>

<强> MainActivity.java

import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends AppCompatActivity {

    SearchView searchView;

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

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView=(SearchView) findViewById(R.id.searchbox);
        searchView.setFocusable(true);// searchView is null
        searchView.setFocusableInTouchMode(true);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是与它相比较的工作以及compare the import widget