Searchview在Android中不起作用

时间:2017-04-10 06:57:27

标签: android searchview

我在android中使用searchview但是它给了我错误的空对象引用 机器人。  我正在从listview中搜索价值,但它给出了错误。请帮帮我 这个。 它给出了这个错误 无法启动活动ComponentInfo {com.hotel.samarthcomp.restrosoft / com.hotel.samarthcomp.restrosoft.Submenu.TwoList}:java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.widget .SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView $ OnQueryTextListener)'在空对象引用上

public class TwoList extends AppCompatActivity {
private final String TAG = this.getClass().getName();

ListView leftList;
String cntforb;
ListView rightList;
ArrayAdapter<String> adapter;
Context context;

ArrayList<String> data;
ArrayAdapter<String> adapter1;
ArrayList<String> data1;
Button GetOrder, place_orderbutton;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private DataForTempNew mydatabase;
private DatabaseForOrderlist databaseforrate;
private DataBaseHandlerForMenu menudatafor;
AlertDialog.Builder alertDialog;
EditText searchmenu;
int _id;
SearchView editsearch;
int value;
String convertedvalue;
HashMap<Integer, Integer> hasmap = new HashMap<Integer, Integer>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row);
    Toolbar toolbar = (Toolbar) findViewById(R.id.product_toolBar_title);
    setSupportActionBar(toolbar);
    //place_orderbutton=(Button)findViewById(R.id.button_6);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(TwoList.this, MainActivity.class);
            i.putExtra("Uname", uname);
            i.putExtra("ForButton", cntforb);
            startActivity(i);
        }
    });

    //searchmenu = (EditText) findViewById(R.id.inputSearch);
    editsearch = (SearchView) findViewById(R.id.search);
    tablenoset = (TextView) findViewById(R.id.tableno_ofneworder);
    orderidset = (TextView) findViewById(R.id.tableorder_id);
    leftList = (ListView) findViewById(R.id.lstLeft);
    //tablenoset.setText(str);
    getTableno();
    context = this;


    leftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedFromList = (leftList.getItemAtPosition(position).toString());

            InsertCategoryFrom insertfromhere = new InsertCategoryFrom(selectedFromList);
            new AsyncCreateCategory().execute(insertfromhere);
            LayoutInflater inflater = getLayoutInflater();
            View convertView = (View) inflater.inflate(R.layout.nnsample, null);
            alertDialog.setTitle("Menu");
            rightList = (ListView) convertView.findViewById(R.id.lstRight);
            populateRightList();
            //alertDialog.setView(searchmenu);
            alertDialog.setView(convertView);
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            alertDialog.show();


        }
    });
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            AlertDialog alert = alertDialog.create();
            alert.dismiss();
        }
    });

    populateLeftList();
    editsearch.setQueryHint("SearchView");

    //*** setOnQueryTextFocusChangeListener ***
    editsearch.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
                    Toast.LENGTH_SHORT).show();
        }
    });
    //*** setOnQueryTextListener ***
    editsearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), query,
                    Toast.LENGTH_SHORT).show();

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            TwoList.this.adapter1.getFilter().filter(newText);
            Toast.makeText(getBaseContext(), newText,
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您还没有在您的代码中的任何位置初始化您的adapter1,这就是为什么它会抛出nullPointerException。