Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

hi, i am new to android studio. I am trying to go to the next window, but when i click the button the app crashes. here is my code

package com.example.getorganized;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;

public class add extends AppCompatActivity {
   private  FirebaseAuth firebaseAuth;
   private  ProgressDialog progressDialo;

    Button cAdd;
    EditText cname;
    String category = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        firebaseAuth = FirebaseAuth.getInstance();
        progressDialo =new ProgressDialog(this);
        progressDialo.setTitle("Please wait");
        progressDialo.setCanceledOnTouchOutside(false);
        cname.findViewById(R.id.CName);
        cAdd.findViewById(R.id.cAdd);

        cAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vaildateData();
            }
        });
    }


    private void vaildateData() {

         category = cname.getText().toString().trim();
        if(category.isEmpty()){
            cname.setError("Enter name of Category");
        }
        else{
            addTo();
        }
    }

    private void addTo() {
        progressDialo.setMessage("Adding category...");
        progressDialo.show();

        long timestamp = System.currentTimeMillis();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", "" +timestamp);
        hashMap.put("category", ""+category);
        hashMap.put("timestamp", timestamp);
        hashMap.put("uid", ""+ firebaseAuth.getUid());

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Categories");
        ref.child(""+timestamp).setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unused) {
                progressDialo.dismiss();
                Toast.makeText(add.this, "Category added successfully", Toast.LENGTH_LONG).show();
            }
        });

    }
}

hi, i am new to android studio. i am trying to create a library management app that connects to firebase. i have already create a login and registration page. I am now trying to go to the next window, but when i click the button the app crashes. here is my code. this is the activity it crashes at. if i remove the following code the app does no crash.
may someone please assist me.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

in your onCreate() method, make the below changes

add

cname = findViewById(R.id.CName);
cAdd = findViewById(R.id.cAdd);

instead of

cname.findViewById(R.id.CName);
cAdd.findViewById(R.id.cAdd);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading