在声明和定义会议室数据库对象时遇到问题

时间:2019-02-17 17:09:56

标签: android android-room

一段时间以来,我一直在面对这个问题。我想将数据插入会议室数据库,但无法正常工作。我正在通过数据访问对象(DAO)插入数据。我做错了某些事情。通过DAO进行方法调用。

UserSignup.java

public class UserSignup extends AppCompatActivity {
EditText fname,lname,contact,mail,password;
CardView signup;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_signup);
    fname = (EditText)findViewById(R.id.user_fname);
    lname = (EditText)findViewById(R.id.user_lname);
    contact = (EditText)findViewById(R.id.user_contact);
    mail = (EditText)findViewById(R.id.user_mail);
    password = (EditText)findViewById(R.id.user_pswd);
    signup = (CardView)findViewById(R.id.create_acc_card);

    signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("debug","OnClickListener Called");
                 String first_name = fname.getText().toString();
            String last_name = lname.getText().toString();
            long Cont = Integer.parseInt(contact.getText().toString());
            String email = mail.getText().toString();
            String pswd = password.getText().toString();
 UserDetailsTable user_info = new UserDetailsTable();
            user_info.setFirstname(first_name);
            user_info.setLastname(last_name);
            user_info.setContact(Cont);
            user_info.setMail(email);
            user_info.setPassword(pswd);
            if(user_info==null)
                Log.d("debug","User_Info is null");
            try{
//Here is the thing about which I'm confused                   
LoginPage.INSTANCE.userdetailsDAO().AddUser(user_info);
                Intent it = new Intent(UserSignup.this, Pharmacist_Home.class); //Spinner Implementation
                startActivity(it);
            }catch (Exception e){
                Log.d("debug", "Exception: " + e.getMessage() );
            }

            Intent intent = new Intent(UserSignup.this,Pharmacist_Home.class);
            } 
        }
    });
}
}

这就是我被困住的地方

 LoginPage.INSTANCE.userdetailsDAO().AddUser(user_info);

我已经在 Login.java

中声明了 INSTANCE

LoginPage.java

 public static MyAppDatabase INSTANCE;
       INSTANCE = MyAppDatabase.getInstance(this);

这是MyAppDatabase.java,我在其中初始化了INSTANCE

MyAppDatabase.java

private static MyAppDatabase instance;
public static MyAppDatabase getInstance(Context context) {
    //Instance Initialized
    if (instance == null) {
        synchronized (MyAppDatabase.class) {
            if (instance == null) {
                instance = Room.databaseBuilder(context.getApplicationContext(),
                        MyAppDatabase.class, DB_NAME).build();
            }
        }
    }
    return instance;

我深陷其中。建议将不胜感激。如果还有其他方法也可以指导我。谢谢

0 个答案:

没有答案