简体   繁体   English

无法通过片段按钮的onClickListener调用片段中的EditText.getText()

[英]Can't call EditText.getText() in fragment via fragment Button's onClickListener

I have a fragment with an EditText and a Button : 我有一个带有EditTextButton的片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".activity.main" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/etLocation"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btnSearch"
            android:text="Search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-aukto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.app.myapp.activity.main" >
    </FrameLayout>
</LinearLayout>

And from my fragment I am trying to set up an onClickListener for that button so that I can use the text from the etLocation when I click btnSearch : 从我的片段中,我试图为该按钮设置一个onClickListener ,以便单击btnSearch时可以使用etLocation的文本:

package com.app.myapp.fragment;

import ...

public class main_fragment extends Fragment implements OnMapReadyCallback, View.OnClickListener{
    GoogleMap googleMap;

public main_fragment(){
    // Required empty public constructor
}

@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_main_fragment, container, false);
    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    if(mapFragment == null){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mapFragment = SupportMapFragment.newInstance();
        fragmentTransaction.replace(R.id.map, mapFragment).commit();
    }
    mapFragment.getMapAsync(this);
    Button btnSearch = view.findViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(this);
    return view;
}

@Override public void onMapReady(GoogleMap googleMap){
    this.googleMap = googleMap;
}

@Override public void onClick(View v){
    EditText etLocation = v.findViewById(R.id.etLocation);
    String location = etLocation.getText().toString();
}

My fragment is being displayed within Activity main, and I can see the Button and TextView , but when I click the button my app crashes. 我的片段显示在Activity main中,并且可以看到ButtonTextView ,但是当我单击按钮时,我的应用程序崩溃了。

If I comment out the getText() line like this, my app does not crash: 如果我这样注释掉getText()行,则我的应用程序不会崩溃:

@Override public void onClick(View v){
    EditText etLocation = v.findViewById(R.id.etLocation);
    //String location = etLocation.getText().toString();
}

So I am confident that the getText() is causing the app to crash. 因此,我相信getText()会导致应用崩溃。

using setText() in this way also causes the app to crash. 以这种方式使用setText()也会导致应用崩溃。

Why can't I call getText() (or setText() ) in this way? 为什么不能以这种方式调用getText() (或setText() )?

Because "v" inside the onClickListener is the clicked View in this case your button.This means you are trying to find a view by id inside the button, which in this case cant happened because a button is not a ViewGroup. 因为onClickListener内的“ v”是这种情况下您单击的按钮。这意味着您试图通过id在按钮内查找视图,在这种情况下,因为按钮不是ViewGroup,所以无法发生这种情况。 You have a couple of ways to do this: 您有两种方法可以做到这一点:

  1. Declare the EditText as a field and find it wherever you find your views 将EditText声明为字段,然后在任何可以找到视图的地方找到它
  2. Before you find btnSearch fins the EditeText as a local final variable, set the click listener to the button as an anonymous interface setOnClickListener(new View.OnCl..). 在找到btnSearch并将EditeText查找为局部最终变量之前,请将单击侦听器设置为该按钮作为匿名接口setOnClickListener(new View.OnCl ..)。 Use the EditText variable inside that click listener 在点击监听器中使用EditText变量
  3. Like you are doing it now you can use getView() instead of "v". 就像现在一样,您可以使用getView()代替“ v”。 getView method in a Fragment gives you the Fragment View 片段中的getView方法为您提供了片段视图

As general recommendation in Fragments, Views should be find in the onViewCreated method, you have to override it. 作为Fragments中的一般建议,应该在onViewCreated方法中找到Views,您必须重写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM