Home > Software design >  hi, i am new to android studio. I am trying to go to the next window, but when i click the button th
hi, i am new to android studio. I am trying to go to the next window, but when i click the button th

Time:06-07

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.

CodePudding user response:

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);
  • Related