令人沮丧的App崩溃

时间:2018-02-26 21:52:59

标签: android firebase firebase-realtime-database

之前让我的应用程序完美地,现在它已经不断崩溃,因为我试图添加额外的,但功能几乎相同。通过额外的功能,我的意思是编辑文本,旋转器等。这非常令人沮丧,因为它显然是非常小的似乎是问题。

我的LogCat显示我的Property类的第114行出现错误,但是,我无法理解为什么这会出现问题,因为它在实现添加功能之前不会影响应用程序。

LogCat

    02-26 21:19:20.424 9478-9478/com.example.benchalmers.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.benchalmers.myapplication, PID: 9478
                                                                                     com.google.firebase.database.DatabaseException: Class com.example.benchalmers.myapplication.Property does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
                                                                                         at com.google.android.gms.internal.zzelx.zze(Unknown Source:51)
                                                                                         at com.google.android.gms.internal.zzelw.zzb(Unknown Source:772)
                                                                                         at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
                                                                                         at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                                                         at com.example.benchalmers.myapplication.PropertyActivity$4.onDataChange(PropertyActivity.java:114)
                                                                                         at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
                                                                                         at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
                                                                                         at com.google.android.gms.internal.zzeig.run(Unknown Source:65)
                                                                                         at android.os.Handler.handleCallback(Handler.java:790)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
02-26 21:19:20.425 9478-9478/com.example.benchalmers.myapplication E/UncaughtException: com.google.firebase.database.DatabaseException: Class com.example.benchalmers.myapplication.Property does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
                                                                                            at com.google.android.gms.internal.zzelx.zze(Unknown Source:51)
                                                                                            at com.google.android.gms.internal.zzelw.zzb(Unknown Source:772)
                                                                                            at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
                                                                                            at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                                                            at com.example.benchalmers.myapplication.PropertyActivity$4.onDataChange(PropertyActivity.java:114)
                                                                                            at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
                                                                                            at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
                                                                                            at com.google.android.gms.internal.zzeig.run(Unknown Source:65)
                                                                                            at android.os.Handler.handleCallback(Handler.java:790)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

在进行这些更改之前,我能够完美地添加,更新和删除记录,没有任何打嗝。这就是让我相信我遇到的问题很容易解决的原因。

当屏幕加载1秒后,它几乎立即崩溃。

我对变量名等做了一些小改动,认为这可能有所帮助,但无济于事。

对此的任何帮助将不胜感激。我是Android的新手,还有Stack!第一篇文章!

PropertyActivity.java

    package com.example.benchalmers.myapplication;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class PropertyActivity extends AppCompatActivity {

    public static final String PROPERTY_NAME = "propertyname";
    public static final String PROPERTY_ID = "propertyid";

    EditText editTextProperty;
    EditText editTextPostcode;
    EditText editTextBedrooms;
    EditText editTextBathrooms;
    Button buttonAddProperty;
    Spinner spinnerFuel;
    Spinner spinnerStatus;
    Spinner spinnerEPC;

    DatabaseReference databaseProperties;

    ListView listViewProperties;

    List<Property> propertyList;

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

        databaseProperties = FirebaseDatabase.getInstance().getReference("properties");

        editTextProperty = (EditText) findViewById(R.id.editTextProperty);
        editTextPostcode = (EditText) findViewById(R.id.editTextPostcode);
        editTextBedrooms = (EditText) findViewById(R.id.editTextBedrooms);
        editTextBathrooms = (EditText) findViewById(R.id.editTextBathrooms);
        buttonAddProperty = (Button) findViewById(R.id.buttonAddProperty);
        spinnerStatus = (Spinner) findViewById(R.id.spinnerStatus);
        spinnerEPC = (Spinner) findViewById(R.id.spinnerEPC);
        spinnerFuel = (Spinner) findViewById(R.id.spinnerFuel);

        listViewProperties = (ListView) findViewById(R.id.listViewProperties);

        propertyList = new ArrayList<>();

        buttonAddProperty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addProperty();

            }
        });

        listViewProperties.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                Property property = propertyList.get(position);
                Intent intent = new Intent(getApplicationContext(), AddTenantsActivity.class);

                intent.putExtra(PROPERTY_ID, property.getPropertyId());
                intent.putExtra(PROPERTY_NAME, property.getPropertyAddress());

                startActivity(intent);
            }
        });

        listViewProperties.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Property property = propertyList.get(position);

                showUpdateDialog(property.getPropertyId(), property.getPropertyAddress(), property.getPropertyPostcode(), property.getPropertyBedrooms(), property.getPropertyBathrooms(), property.getPropertyStatus(), property.getPropertyEPC(), property.getPropertyFuel());
                return false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseProperties.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                propertyList.clear();

                for(DataSnapshot propertySnapshot : dataSnapshot.getChildren()){
                    Property property = propertySnapshot.getValue(Property.class);

                    propertyList.add(property);

                }

                PropertyList adapter = new PropertyList (PropertyActivity.this, propertyList);
                listViewProperties.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void showUpdateDialog(final String id, String propertyAddress, String propertyPostcode, String propertyBedrooms, String propertyBathrooms, String propertyStatus, final String propertyId, String propertyName) {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.update_dialog, null);

        dialogBuilder.setView(dialogView);

        final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);
        final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdate);
        final Spinner spinnerFuel = (Spinner) dialogView.findViewById(R.id.spinnerFuel);
        final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDelete);

        dialogBuilder.setTitle("Updating Property " + propertyName);

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = editTextName.getText().toString().trim();
                String postcode = editTextPostcode.getText().toString().trim();
                String bedrooms = editTextBedrooms.getText().toString().trim();
                String bathrooms = editTextBathrooms.getText().toString().trim();
                String status = spinnerStatus.getSelectedItem().toString();
                String epc = spinnerEPC.getSelectedItem().toString();
                String fuel = spinnerFuel.getSelectedItem().toString();

                if(TextUtils.isEmpty(name)){

                    editTextName.setError("Name Required");
                    return;
                }

                updateProperty(propertyId, status, postcode, bedrooms, bathrooms, id, name, epc, fuel);

                alertDialog.dismiss();
            }
        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteProperty(propertyId);
            }
        });

    }

    private void deleteProperty(String propertyId) {
        DatabaseReference drProperty = FirebaseDatabase.getInstance().getReference("properties").child(propertyId);
        DatabaseReference drTenants = FirebaseDatabase.getInstance().getReference("tenants").child(propertyId);

        drProperty.removeValue();
        drTenants.removeValue();

        Toast.makeText(this, "Property has been deleted", Toast.LENGTH_LONG).show();

    }

    private boolean updateProperty(String propertyId, String status, String postcode, String bedrooms, String bathrooms, String id, String name, String epc, String fuel){

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("properties").child(id);
        Property property = new Property (id, name, postcode, bedrooms, bathrooms, status, epc, fuel);

        databaseReference.setValue(property);

        Toast.makeText(this, "Property Updated Successfully", Toast.LENGTH_LONG).show();

        return true;
    }

    private void addProperty(){

        String address = editTextProperty.getText().toString().trim();
        String postcode = editTextPostcode.getText().toString().trim();
        String bedrooms = editTextBedrooms.getText().toString().trim();
        String bathrooms = editTextBathrooms.getText().toString().trim();
        String status = spinnerStatus.getSelectedItem().toString();
        String epc = spinnerEPC.getSelectedItem().toString();
        String fuel = spinnerFuel.getSelectedItem().toString();

        if(!TextUtils.isEmpty(address)){

            String id = databaseProperties.push().getKey();

            Property property = new Property (id, address, postcode, bedrooms, bathrooms, status, epc, fuel);

            databaseProperties.child(id).setValue(property);

            Toast.makeText(this, "Property Added", Toast.LENGTH_LONG).show();

        }else {

            Toast.makeText(this, "You must enter a property", Toast.LENGTH_LONG).show();
        }

    }
}

Property.java

package com.example.benchalmers.myapplication;

/**
 * Created by benchalmers on 21/02/2018.
 */

public class Property {

    String propertyId;
    String propertyAddress;
    String propertyPostcode;
    String propertyFuel;
    String propertyBedrooms;
    String propertyBathrooms;
    String propertyEPC;
    String propertyStatus;

    public Property(String id, String address, String postcode, String bedrooms, String bathrooms, String status, String epc, String fuel ) {

        this.propertyId = propertyId;
        this.propertyAddress = propertyAddress;
        this.propertyPostcode = postcode;
        this.propertyFuel = propertyFuel;
        this.propertyBedrooms = bedrooms;
        this.propertyBathrooms = bathrooms;
        this.propertyEPC = epc;
        this.propertyStatus = status;
    }

    public String getPropertyId() {
        return propertyId;
    }

    public String getPropertyAddress() {
        return propertyAddress;
    }

    public String getPropertyPostcode() {
        return propertyPostcode;
    }

    public String getPropertyFuel() {
        return propertyFuel;
    }

    public String getPropertyBedrooms() {
        return propertyBedrooms;
    }

    public String getPropertyBathrooms() {
        return propertyBathrooms;
    }

    public String getPropertyEPC() {
        return propertyEPC;
    }

    public String getPropertyStatus() {
        return propertyStatus;
    }
}

PropertyList.java

package com.example.benchalmers.myapplication;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class PropertyList extends ArrayAdapter<Property> {

    private Activity context;
    private List<Property> propertyList;

    public PropertyList(Activity context, List<Property> propertyList) {

        super(context, R.layout.property_list_layout, propertyList);
        this.context = context;
        this.propertyList = propertyList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View listViewItem = inflater.inflate(R.layout.property_list_layout, null, true);

        TextView textViewProperty = (TextView) listViewItem.findViewById(R.id.textViewProperty);
        TextView textViewPostcode = (TextView) listViewItem.findViewById(R.id.textViewPostcode);
        TextView textViewBedrooms = (TextView) listViewItem.findViewById(R.id.textViewBedrooms);
        TextView textViewBathrooms = (TextView) listViewItem.findViewById(R.id.textViewBathrooms);
        TextView textViewStatus = (TextView) listViewItem.findViewById(R.id.textViewStatus);
        TextView textViewEPC = (TextView) listViewItem.findViewById(R.id.textViewEPC);
        TextView textViewFuel = (TextView) listViewItem.findViewById(R.id.textViewFuel);


        Property property = propertyList.get(position);

        textViewProperty.setText(property.getPropertyAddress());
        textViewPostcode.setText(property.getPropertyPostcode());
        textViewBedrooms.setText(property.getPropertyBedrooms());
        textViewBathrooms.setText(property.getPropertyBathrooms());
        textViewStatus.setText(property.getPropertyStatus());
        textViewEPC.setText(property.getPropertyEPC());
        textViewFuel.setText(property.getPropertyFuel());

        return listViewItem;
    }
}

1 个答案:

答案 0 :(得分:0)

Firebase需要Property类中的无参数构造函数,以便您可以使用getValue(Property.class)。因此,将此构造函数添加到Property类:

public Property() {}
相关问题