AutoIncrement Realm失败

时间:2016-06-05 14:38:18

标签: java android oop realm

我创建了一个活动,并希望插入一个新的领域对象 如果我把getNextKey();

,它总是保持错误http null

这是我的活动

package com.example.rikirikmen.billsplit;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.rikirikmen.billsplit.Adapter.BillListAdapter;
import com.example.rikirikmen.billsplit.Adapter.MenuAdapter;
import com.example.rikirikmen.billsplit.Model.Bill;
import com.example.rikirikmen.billsplit.Model.DetailMenu;
import com.example.rikirikmen.billsplit.Model.DetailPerson;
import com.example.rikirikmen.billsplit.Model.PersonInMenu;
import com.example.rikirikmen.billsplit.Model.PersonMenuObj;

import org.w3c.dom.Text;

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmList;
import io.realm.RealmObject;

public class DialogActivity extends AppCompatActivity {
Realm realm;
private RealmList<DetailPerson> person;
private RealmList<PersonInMenu> pim;
private ListView listView;
private MenuAdapter adapter;
private TextView menuName;
private TextView menuPrice;
private TextView menuQty;
private String bill;
private Button btnOke;
private Button btnCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_add_menu);
    realm = Realm.getDefaultInstance();
    bill = getIntent().getStringExtra("bill_ID");
    btnOke = (Button) findViewById(R.id.btnOkMenu);
    btnCancel = (Button) findViewById(R.id.btnCancelMenu);
    menuName = (TextView) findViewById(R.id.txtMenuName);
    menuPrice = (TextView) findViewById(R.id.txtMenuPrice);
    menuQty = (TextView) findViewById(R.id.txtMenuQuantity);
    listView = (ListView) findViewById(R.id.listViewMenu);

    Toast.makeText(this, getNextKey(), Toast.LENGTH_SHORT).show();

    person = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst().getDetailperson();

        if (person.isEmpty()){


        }
        else{
            adapter = new MenuAdapter(this,R.layout.row_menu, person,bill);
            listView.setAdapter(adapter);
        }

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

            if (menuName.getText().length()==0 || menuPrice.getText().length()==0|| menuQty.getText().length()==0){
                Toast.makeText(DialogActivity.this, "Please fill the menu detail", Toast.LENGTH_SHORT).show();

            }
            else {
                CharSequence priceText = menuPrice.getText();
                int Price = Integer.parseInt(priceText.toString());

                CharSequence qtyText = menuQty.getText();
                int Qty = Integer.parseInt(qtyText.toString());

                realm.beginTransaction();
                DetailMenu menu = realm.createObject(DetailMenu.class);
                menu.setMenuID(getNextKey());
                menu.setMenuName(String.valueOf(menuName.getText()));
                menu.setMenuPrice(Price);
                menu.setQuantity(Qty);

                    for (int i = 0; i < adapter.getPersonMenuObjList().size(); i++) {
                        PersonInMenu pim = realm.createObject(PersonInMenu.class);
                        pim.setPersonID(adapter.getPersonMenuObjList().get(i).getPersonID());
                        pim.setStatus(adapter.getPersonMenuObjList().get(i).isStatus());
                        menu.personInMenus.add(pim);
                    }


                realm.copyToRealmOrUpdate(menu);
                realm.commitTransaction();
                realm.close();


            }
        }
    });



}
int getNextKey()
{
    return realm.where(DetailMenu.class).max("MenuID").intValue() + 1;
}

}

这是我的DetailMenu.class

package com.example.rikirikmen.billsplit.Model;


import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

/**
 * Created by rikirikmen on 4/24/2016.
 */
public class DetailMenu extends RealmObject {

    @PrimaryKey
    private int MenuID;
    private int Quantity;
    private String MenuName;
    private int MenuPrice;
    public RealmList<PersonInMenu> personInMenus;

    public RealmList<PersonInMenu> getPersonInMenus() {
        return personInMenus;
    }

    public void setPersonInMenus(RealmList<PersonInMenu> personInMenus) {
        this.personInMenus = personInMenus;
    }

    public DetailMenu() {
        super();
    }

    public int getMenuPrice() {
        return MenuPrice;
    }

    public void setMenuPrice(int menuPrice) {
        MenuPrice = menuPrice;
    }

    public int getMenuID() {
        return MenuID;
    }

    public void setMenuID(int menuID) {
        MenuID = menuID;
    }

    public String getMenuName() {
        return MenuName;
    }

    public void setMenuName(String menuName) {
        MenuName = menuName;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setQuantity(int quantity) {
        Quantity = quantity;
    }


}

我不知道为什么我的getNextKey()不起作用,因为我在其他活动中使用相同的方式而且它有效。这里有人和我有同样的问题吗?

1 个答案:

答案 0 :(得分:1)

如果没有对象,

max会返回一个数字或null。所以很可能不存在DetailMenu对象,并且你得到一个NullPointerException。

请改用:

Number max = realm.where(DetailMenu.class).max("MenuID");
return (max != null) ? max.intValue() + 1 : 0;