0

I am using searchview in android but it gives me error null object reference in android. i am searching value from listview but it gives error. Please help me for this. It gives this error Unable to start activity ComponentInfo{com.hotel.samarthcomp.restrosoft/com.hotel.samarthcomp.restrosoft.Submenu.TwoList}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference

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;
        }
    });
}
Dipak
  • 1
  • 4
  • It would seem that the `` with ID `search` is not in the `row` layout, either directly, or in an ``d layout. – Mike M. Apr 10 '17 at 07:01

1 Answers1

0

You have not initialized your adapter1 anywhere in your code, thats why its throwing nullPointerException.

Anmol317
  • 1,356
  • 1
  • 14
  • 31