我正在尝试从片段 xml 中获取 id 所以这里是错误代码“无法解析 findViewById 方法”
有没有我可以findViewById在 Fragment 中使用的?
public class Slide_Teacher_Add extends Fragment implements View.OnClickListener {
private EditText teacher_id_number;
private EditText teacher_fullname;
private EditText teacher_lesson;
private EditText teacher_contact;
private Button btn_add_teacher;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
return v;
teacher_id_number = (EditText) findViewById(R.id.teacher_id_number);
teacher_fullname = (EditText) findViewById(R.id.teacher_fullname);
teacher_lesson = (EditText) findViewById(R.id.teacher_lesson);
teacher_contact = (EditText) findViewById(R.id.teacher_contact);
btn_add_teacher = (Button) findViewById(R.id.btn_add_teacher);
btn_add_teacher.setOnClickListener(this);
}
Run Code Online (Sandbox Code Playgroud) 当我按下活动上的按钮时,将调用我的DialogFragment类.我希望通过此DialogFragment设置的日期显示在按钮文本上.FindViewById拒绝被类识别.
我发现了一个类似的问题,但我无法将其与我的案例联系起来.
码:
调用Dialogue片段的按钮:
public void datepicker(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
Run Code Online (Sandbox Code Playgroud)对话片段代码:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceSateate) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen
}
}
}
Run Code Online (Sandbox Code Playgroud)我在谷歌以及StackOverflow上搜索过.但是没有一个解决方案解决了我的问题.findViewById在onCreate中返回null.但是,当我将其更改为onClick函数时,它工作正常.请帮我.我是android编程的初学者.
我的MainFunction.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
final TextView t = (TextView) findViewById(R.id.textView1);
t.setText("hi world.");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically …Run Code Online (Sandbox Code Playgroud) 我正在创建一个新的Tab界面,对于每个选项卡,我都有以下功能:
public class ClassViewFragment extends Fragment {
ListView classListView = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) findViewById(R.id.classList);
return rootView;
}
}
Run Code Online (Sandbox Code Playgroud)
但我cannot resolve method findViewById也在另一个地方,我也无法使用runOnUiThread和获取Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)
我知道如果我的课程扩展了,这些工作正常,Activity但是它是一个标签片段,那我该如何使用这个功能呢?
我需要在MvxFragment中找到我在axml中声明的视图.在我可以打电话的活动FindViewById<T>(RESOURCE_ID)中OnCreate.这不适用于片段.
这就是它如何与普通的Android片段一起使用:片段中的findViewById
如何在MvxFragment不丢失绑定上下文的情况下对视图进行充气?
可能重复:
片段android中的findViewById
我目前正在研究android项目并试图让片段工作.片段部分正在工作,但我正在尝试控制UI组件,在标准活动上我可以使用类似的东西
TextView txtMyTextBox = (TextView)findViewById(R.id.my_text_box)
Run Code Online (Sandbox Code Playgroud)
我正在通过ListFragment扩展类,但是为什么我尝试与上面相同的代码,我得到以下错误
findViewById(int)方法未定义
为什么这不起作用.感谢您的任何帮助,您可以提供.