[英]Can't call EditText.getText() in fragment via fragment Button's onClickListener
I have a fragment with an EditText
and a Button
: 我有一个带有
EditText
和Button
的片段:
<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中,并且可以看到Button
和TextView
,但是当我单击按钮时,我的应用程序崩溃了。
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:
您有两种方法可以做到这一点:
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.