Home > Net >  Error when i click the button for move to the next page
Error when i click the button for move to the next page

Time:12-23

When i click on button, this is happening:

https://i.stack.imgur.com/PeAAG.png

The button is for getting the value from the searchView when the user fill it. And for open the next activity. onclick:

 String state;
 SearchView searchView;
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
searchView=(SearchView)findViewById(R.id.search_product);
        Button get_product=findViewById(R.id.get_product);
        get_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String product=searchView.toString();
                Intent intent = new Intent(SearchActivity.this, The_End.class);
                intent.putExtra("product", product);
                Toast.makeText(getApplicationContext(),product,Toast.LENGTH_SHORT).show();

                startActivity(intent);

            }

        });
        }

Here is my xml with button and searchView:

    <SearchView
        android:id="@ id/search_product"
        android:layout_width="249dp"
        android:layout_height="37dp"
        android:background="#CCCDFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.285" />
<Button
        android:id="@ id/get_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.839"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/search_product"
        app:layout_constraintVertical_bias="0.885" />

CodePudding user response:

In String product=searchView.toString(); you are not fetching the value of searchview, you are fetching refrence of searchview and converting it to string using toString() which should not be done.

To get the contents of searchview you need to use searchView.getQuery() and then put the result in your intent.putExtra("product", searchView.getQuery());

CodePudding user response:

String product = searchView.toString();

that provides you viewId for SearchView used.Instead to get text from searchView you need to use

String product = searchView.getQuery().toString();
  • Related