0
I have a Fragment that through a button it will call the other Fragment. But the following error is appearing.
06-20 19:12:12.075 17971-17971/com.example.gerdaumanagement.gerdaumanagement E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gerdaumanagement.gerdaumanagement, PID: 17971
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.gerdaumanagement.gerdaumanagement.adicionar_usuario.<init>(adicionar_usuario.java:158)
at com.example.gerdaumanagement.gerdaumanagement.usuario.adicionarUsuario(usuario.java:42)
at com.example.gerdaumanagement.gerdaumanagement.usuario$1.onClick(usuario.java:29)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
being that the Fragment that calls the other Fragment is:
public class usuario extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_usuario, container, false);
// ListView listaUsuario = (ListView) rootView.findViewById(R.id.listaUsuario);
Button botao = (Button) rootView.findViewById(R.id.addUser);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adicionarUsuario(v);
}
});
// Inflate the layout for this fragment
return rootView;
}
public void adicionarUsuario(View view){
adicionar_usuario adduser = new adicionar_usuario();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.content,adduser, adduser.getTag()).addToBackStack(null).commit();
}
}
According to the error shown, you have error in line 42, which is the excerpt: adicionar_usuario adduser = new adicionar_usuario();
and my Fragment to be called is the following:
public class adicionar_usuario extends Fragment {
public adicionar_usuario() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String TAG = "myApp";
Log.d(TAG, "ENTROU!");
View rootView = inflater.inflate(R.layout.fragment_adicionar_usuario, container, false);
Button botao = (Button) rootView.findViewById(R.id.salvar);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
salvarUsuario(v);
}
});
//Alterar nome da actionbar
((MenuDrawer) getActivity()).setActionBarTitle("Adicionar Usuário");
//TiPOS
Spinner spinner = (Spinner) rootView.findViewById(R.id.tipoFunc);
ArrayAdapter<CharSequence> adapterTiposFunc = ArrayAdapter.createFromResource(getActivity(), R.array.tipoFunc, android.R.layout.select_dialog_item);
adapterTiposFunc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterTiposFunc);
Intent intent = getActivity().getIntent();
if(intent != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
usuario.setId(bundle.getInt("id"));
usuario.setNome(bundle.getString("nome"));
usuario.setEmail(bundle.getString("email"));
usuario.setTipoFunc(bundle.getString("tipoFunc"));
usuario.setNp(bundle.getString("np"));
//nomeEt.setText(usuario.getNome());
emailEt.setText(usuario.getEmail());
// senhaEt.setVisibility(View.GONE);
salvarBt.setVisibility(View.GONE);
//editarBt.setVisibility(View.VISIBLE);
}
}
// Inflate the layout for this fragment
return rootView;
}
public class usuarioData {
private String nome;
private String email;
private String np;
private String tipoFunc;
private String senha;
private int id;
/* public usuarioData(){
this.nome = nome;
this.email=email;
this.np=np;
this.tipoFunc = tipoFunc;
this.senha = senha;
this.id = id;
}*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNp() {
return np;
}
public void setNp(String np) {
this.np = np;
}
public String getTipoFunc() {
return tipoFunc;
}
public void setTipoFunc(String tipoFunc) {
this.tipoFunc = tipoFunc;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
private usuarioData usuario = new usuarioData();
private EditText nomeEt = (EditText) getView().findViewById(nome);
private EditText emailEt = (EditText) getView().findViewById(email);
private EditText npEt = (EditText) getView().findViewById(np);
private Spinner tipoFuncSp = (Spinner) getView().findViewById(tipoFunc);
private Button salvarBt = (Button) getView().findViewById(salvar);
public void salvarUsuario(View view){
//usuario.setNome(nomeEt.getText().toString());
usuario.setEmail(emailEt.getText().toString());
usuario.setTipoFunc(tipoFuncSp.toString());
usuario.setNp(npEt.getText().toString());
db_funcao bd = new db_funcao(getContext());
bd.inserir(usuario);
Toast.makeText(getActivity(), "Usuário inserido com sucesso!", Toast.LENGTH_SHORT).show();
}
}
and following the error presented in logcat, it has an error in this line: ` private Edittext nameEt = (Edittext) getView(). findViewById(name);
` Does anyone know why the problem is?