无法从 Fragment 访问 TextView
Impossible to reach TextView from Fragment
我在获取片段中的 Texview 引用时遇到了一些问题,我真的不知道为什么。
这是我的layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvLogout"
android:layout_below="@+id/tvSettings"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
这里是 Java Class
public class TabPersonnalSpace extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_personnal_space, container, false);
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
Log.d("Debug", tvSettings.getText().toString());
tvSettings.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT);
}
});
return rootView;
}
}
事情是:当 swipe
到 Fragment 时,我将文本打印到 logcat,而当我单击 TextView 时,什么都没有发生。
有人知道我的线索吗?
你忘了.show()
显示Toast
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT).show();
像这样实现 onViewCreated :
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//instantiate tvSettings
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
}
我在获取片段中的 Texview 引用时遇到了一些问题,我真的不知道为什么。
这是我的layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvLogout"
android:layout_below="@+id/tvSettings"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
这里是 Java Class
public class TabPersonnalSpace extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_personnal_space, container, false);
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
Log.d("Debug", tvSettings.getText().toString());
tvSettings.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT);
}
});
return rootView;
}
}
事情是:当 swipe
到 Fragment 时,我将文本打印到 logcat,而当我单击 TextView 时,什么都没有发生。
有人知道我的线索吗?
你忘了.show()
显示Toast
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT).show();
像这样实现 onViewCreated :
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//instantiate tvSettings
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
}