Personally I need a help with an error in a method of my code.
Basically I have a FrameLayout that loads two fragments. When you start the APP it loads the first fragment with 3 TextEdits and a button. When you click on the buton, it calls a method that is in the parent activity and captures the data from the first fragment to store in a DB. At this point the exception occurs.
This is the error:
java.lang.NullPointerException: Attempt to invoke virtual method android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl<init>.(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.caioferreira.agoravai.Activity.activity_primeiraExecucao.configuraEmpresa(activity_primeiraExecucao.java:42)
at com.example.caioferreira.agoravai.Fragment.f_Empresa.registrarEmp(f_Empresa.java:43)
at com.example.caioferreira.agoravai.Fragment.f_Empresa$1.onClick(f_Empresa.java:34)
at android.view.View.performClick(View.java:5612)
Activity code where the method is:
public class activity_primeiraExecucao extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primeira_execucao);
fragmentEmpresa = new f_Empresa();
transaction.add(R.id.Frame_primeira_execucao,fragmentEmpresa );
transaction.commit();
}
public f_SiTef fragmentSitef;
public f_Empresa fragmentEmpresa;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
public void configurafragment (){
fragmentSitef = new f_SiTef();
transaction.replace(R.id.Frame_primeira_execucao,fragmentSitef);
transaction.commit();
}
public void configuraEmpresa (){
c_Empresa valida = new c_Empresa();
EditText rz = findViewById(R.id.EDT_razaoSocial);
EditText numeroCNPJ = findViewById(R.id.EDT_CNPJ);
EditText numLicenca = findViewById(R.id.EDT_licenca);
String cnp = numeroCNPJ.getText().toString();
String licenc = numLicenca.getText().toString();
String razao = rz.getText().toString();
boolean cnpvalido;
valida.validaEmpresa(razao, cnp, licenc);
if (valida.retorno== 2){
Toast.makeText(getApplicationContext(), "Por gentileza, preencha todos os campos", Toast.LENGTH_SHORT).show();
}
else {
cnpvalido=valida.isCNPJ(cnp);
if (!cnpvalido){
Toast.makeText(getApplicationContext(), "O CNPJ digitado não é válido", Toast.LENGTH_SHORT).show();
}
else if (valida.retorno == 1){
Toast.makeText(getApplicationContext(), "A licença digitada não é valida para este CNPJ", Toast.LENGTH_SHORT).show();
}
else {
c_dbHelper db = new c_dbHelper( getApplicationContext());
boolean grava = db.gravaEmpresa(db, razao, cnp, licenc);
if(grava){
Toast.makeText(getApplicationContext(), "Registro realizado", Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(this, MainActivity.class);
//startActivity(intent);
//finish();
configurafragment();
}
}
}
}
Fragment code:
public class f_Empresa extends Fragment {
activity_primeiraExecucao activity;
public Button btnEmpresa;
public f_Empresa() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.f_empresa, container, false);
btnEmpresa = view.findViewById(R.id.BTN_registrar_empresa);
btnEmpresa.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
registrarEmp(v);
}
});
return view;
}
public void registrarEmp(View v) {
activity = new activity_primeiraExecucao();
activity.configuraEmpresa();
}
}