gpt4 book ai didi

java - 无法在 public void 函数中使用 findViewById 方法

转载 作者:行者123 更新时间:2023-12-01 17:52:25 26 4
gpt4 key购买 nike

我在android中使用 fragment ,我在这个 fragment 中有一个名为CustomersFragment的 fragment ,我在onCreateView上使用这个方法时使用findViewById方法没问题,但是当我在 public void 函数中使用时出现错误,这是底部的我的 fragment 代码

package com.androiddeft.navigationdrawer.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.androiddeft.navigationdrawer.MyListAdapter;
import com.androiddeft.navigationdrawer.R;
import com.androiddeft.navigationdrawer.Employee;
import com.androiddeft.navigationdrawer.DatabaseHelper;
import com.androiddeft.navigationdrawer.constants.NavigationDrawerConstants;

import java.util.List;


public class CustomersFragment extends Fragment {

private Button btnSubmit;
private Button btnCancel;
private ListView listView;
private MyListAdapter adapter;
private EditText edtSearch, edtName, edtPhone, edtAddress;
private int seletcedEmployeeId;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(NavigationDrawerConstants.TAG_CUSTOMERS);
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
// Create the view from XML layout.
final View view = inflater.inflate(R.layout.fragment_customers, null);
// Perform additional configuration on layout components here.
Button btnAdd = (Button) view.findViewById(R.id.btn_add);
Button btnShowSearch = (Button) view.findViewById(R.id.btn_search);
Button btnCloseSearch = (Button) view.findViewById(R.id.btn_close_search);
Button btnDoSearch = (Button) view.findViewById(R.id.btn_do_search);
btnCancel = (Button) view.findViewById(R.id.btn_cancel);
btnSubmit = (Button) view.findViewById(R.id.btn_submit);

edtSearch = (EditText) view.findViewById(R.id.edt_search);
edtName = (EditText) view.findViewById(R.id.edt_employee_name);
edtPhone = (EditText) view.findViewById(R.id.edt_employee_phone);
edtAddress = (EditText) view.findViewById(R.id.edt_employee_address);

btnAdd.setOnClickListener(onAddClicked);
btnShowSearch.setOnClickListener(onShowSearchClicked);
btnCloseSearch.setOnClickListener(onCloseSearchClicked);
btnDoSearch.setOnClickListener(onDoSearchClicked);
btnCancel.setOnClickListener(onCancelbuttonClicked);

listView = (ListView) view.findViewById(R.id.listView);
refreshList();
return view;
}
private void refreshList() {
List<Employee> employees = new DatabaseHelper(this).getListOfEmployees();
adapter = new MyListAdapter(this, employees);
listView.setAdapter(adapter);
}

public void editEmployee(Employee employee) {
findViewById(R.id.lin_add_search).setVisibility(View.GONE);
findViewById(R.id.add_edit_employee).setVisibility(View.VISIBLE);
btnSubmit.setOnClickListener(onEditEmployeeSubmit);
edtName.setText(employee.getName());
edtPhone.setText(employee.getPhone());
edtAddress.setText(employee.getAddress());
seletcedEmployeeId = employee.getId();
}

private View.OnClickListener onAddClicked = new View.OnClickListener() {
@Override
public void onClick(View view) {
findViewById(R.id.lin_add_search).setVisibility(View.GONE);
findViewById(R.id.add_edit_employee).setVisibility(View.VISIBLE);
btnSubmit.setOnClickListener(onNewEmployeeSubmit);
}
};

private View.OnClickListener onShowSearchClicked = new View.OnClickListener() {
@Override
public void onClick(View view) {
findViewById(R.id.lin_add_search).setVisibility(View.GONE);
findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
}
};

private View.OnClickListener onCloseSearchClicked = new View.OnClickListener() {
@Override
public void onClick(View view) {
findViewById(R.id.lin_add_search).setVisibility(View.VISIBLE);
findViewById(R.id.search_bar).setVisibility(View.GONE);
edtSearch.setText("");
refreshList();
}
};

private View.OnClickListener onDoSearchClicked = new View.OnClickListener() {
@Override
public void onClick(View view) {

String input = edtSearch.getText().toString();
if (input.length() == 0) {
Toast.makeText(getActivity(), "لطفا قسمتی از نام کارمند را بنویسید", Toast.LENGTH_SHORT).show();
return;
}

List<Employee> employees = new DatabaseHelper(getActivity()).searchByName(input);
adapter = new MyListAdapter(getActivity(), employees);
listView.setAdapter(adapter);
Toast.makeText(getActivity(), employees.size() + " کارمند پیدا شد", Toast.LENGTH_SHORT).show();
}
};

private View.OnClickListener onCancelbuttonClicked = new View.OnClickListener() {
@Override
public void onClick(View view) {
edtName.setText("");
edtPhone.setText("");
edtAddress.setText("");
findViewById(R.id.lin_add_search).setVisibility(View.VISIBLE);
findViewById(R.id.add_edit_employee).setVisibility(View.GONE);
}
};

private View.OnClickListener onNewEmployeeSubmit = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edtName.getText().length() == 0 || edtPhone.getText().length() == 0 || edtAddress.getText().length() == 0) {
Toast.makeText(getActivity(), "لطفا اطلاعات را وارد کنید", Toast.LENGTH_SHORT).show();
return;
}
Employee employee = new Employee();
employee.setName(edtName.getText().toString());
employee.setPhone(edtPhone.getText().toString());
employee.setAddress(edtAddress.getText().toString());
new DatabaseHelper(getActivity()).addNewEmployee(employee);
refreshList();
btnCancel.performClick();
}
};

private View.OnClickListener onEditEmployeeSubmit = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edtName.getText().length() == 0 || edtPhone.getText().length() == 0 || edtAddress.getText().length() == 0) {
Toast.makeText(getActivity(), "لطفا اطلاعات را وارد کنید", Toast.LENGTH_SHORT).show();
return;
}
Employee employee = new Employee();
employee.setId(seletcedEmployeeId);
employee.setName(edtName.getText().toString());
employee.setPhone(edtPhone.getText().toString());
employee.setAddress(edtAddress.getText().toString());
new DatabaseHelper(getActivity()).editEmployee(employee);
refreshList();
btnCancel.performClick();
}
};
}

最佳答案

Android 开发人员通常使用两种名为 findViewById() 的方法。其中之一是 Activity 类的一部分,通常按如下方式调用:

public void myMethod() {
View myView = findViewById(R.id.myView);
}

另一个是View类的一部分,通常像这样调用:

public void myMethod(View root) {
View myView = root.findViewById(R.id.myView);
}

但是,Fragment 类中没有 findViewById() 方法,因此您必须使用 View 方法。您已经在 onCreateView() 中执行此操作:

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_customers, null);
Button btnAdd = (Button) view.findViewById(R.id.btn_add);
^^^^^^^^^^^^^^^^^

但是,稍后你会得到这个:

public void editEmployee(Employee employee) {
findViewById(R.id.lin_add_search).setVisibility(View.GONE);
^^^^^^^^^^^^

同样,Fragment 类没有 findViewById() 方法,因此无法编译。通常解决方法是调用 getView().findViewById()getView() 方法返回的 View 实例与您从 onCreateView() 返回的 View 实例相同,因此它可以正常工作。

类似这样的事情:

public void editEmployee(Employee employee) {
getView().findViewById(R.id.lin_add_search).setVisibility(View.GONE);

关于java - 无法在 public void 函数中使用 findViewById 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com