Android 我能';不要使用findviewbyd
为什么我不能在此文件中使用Android 我能';不要使用findviewbyd,android,android-fragments,Android,Android Fragments,为什么我不能在此文件中使用findViewById Regfragment: import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.
findViewById
Regfragment:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class RegFragment extends Fragment {
EditText text1,text2,text3;
Button btn1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
return rootView;
}
}
然后我试着这样做:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class RegFragment extends Fragment {
EditText text1,text2,text3;
Button btn1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
return rootView;
text1 = (EditText) findViewById(R.id.text1); -----> ERROR....
}
}
我有错误是因为我使用了片段吗?
在返回语句之前必须这样做
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText) rootView.findViewById(R.id.text1);
return rootView;
希望它能起作用 您使用了充气机
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
所以使用
text1 = (EditText) rootView.findViewById(R.id.text1);
text2 = (EditText) rootView.findViewById(R.id.text2);
text3 = (EditText) rootView.findViewById(R.id.text3);
等等。您不能在片段中使用它,因为它是活动的方法 试着跟随 在onActivityCreated()中
text1=(EditText)getView().findViewById(R.id.text1)代码>
或者在onCreateView()中
text1=(EditText)rootView.findviewbyd(R.id.text1)
要访问片段中的视图,您需要通过包含布局的视图
类访问它
您需要更改代码,如下所示:
public class RegFragment extends Fragment {
EditText text1,text2,text3;
Button btn1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText)rootView .findViewById(R.id.text1);
//Same way initialize other views also.
return rootView;
}
}
只是一个简单的方法。你已经膨胀了布局。从中继承。查找下面的代码
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText)rootView.findViewById(R.id.text1);
return rootView;
由于您使用了单独的视图来膨胀布局,因此需要使用该视图访问该布局中的所有文本视图/图像视图/编辑文本
因此,要更正代码,您需要调整代码,如下所示
View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText) rootView.findViewById(R.id.text1);
return rootView;
可能会得到unreachable语句
,因为您要在return语句之后初始化textview,所以请使用text1=(EditText)rootView。findViewById(R.id.text1)
在返回语句之前检查我的答案并相应地更改代码。非常感谢您,先生……为什么我没有这样尝试text1=(EditText)getActivity().findViewById(R.id.text1)代码>是错误的,尽管我没有投反对票。应该使用getView()
不正确。错。您不能使用onActivityCreatedwe可以使用且我正在使用的onActivityCreatedwe中的第一个