【问题标题】:Error when i click the button for move to the next page单击按钮移动到下一页时出错
【发布时间】:2022-01-22 22:31:36
【问题描述】:

当我点击按钮时,会发生这种情况:

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

该按钮用于在用户填写时从 searchView 中获取值。并为打开下一个活动。 点击:

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

            }

        });
        }

这是我的带有按钮和 searchView 的 xml:

    <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" />

【问题讨论】:

    标签: java android-studio button searchview


    【解决方案1】:

    String product = searchView.toString();

    为您使用的 SearchView 提供 viewId。而不是从您需要使用的 searchView 中获取文本

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

    【讨论】:

      【解决方案2】:

      String product=searchView.toString(); 中,您不是在获取searchview 的值,而是在获取searchview 的引用并使用toString() 将其转换为字符串。

      要获取 searchview 的内容,您需要使用 searchView.getQuery(),然后将结果放入您的 intent.putExtra("product", searchView.getQuery());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多