Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从EditText获取字符串时,无法通过setter设置类对象的参数,但在传递硬编码字符串时也可以这样做_Java_Android - Fatal编程技术网

Java 从EditText获取字符串时,无法通过setter设置类对象的参数,但在传递硬编码字符串时也可以这样做

Java 从EditText获取字符串时,无法通过setter设置类对象的参数,但在传递硬编码字符串时也可以这样做,java,android,Java,Android,自定义类代码: package com.example.accounts; public class PartyModel { private int parId; private String parName; private String parType; private String parOwner; private String parOwnerContact; public PartyModel() { }

自定义类代码:

package com.example.accounts;
public class PartyModel {

    private int parId;
    private String parName;
    private String parType;
    private String parOwner;
    private String parOwnerContact;

    public PartyModel() {
            }

    public PartyModel(int parId, String parName, String parType, String parOwner, String parOwnerContact) {
        this.parId = parId;
        this.parName = parName;
        this.parType = parType;
        this.parOwner = parOwner;
        this.parOwnerContact = parOwnerContact;
    }

    public int getParId() {
        return parId;
    }

    public void setParId(int parId) {
        this.parId = parId;
    }

    public String getParName() {
        return parName;
    }

    public void setParName(String parName) {
        this.parName = parName;
    }

    public String getParType() {
        return parType;
    }

    public void setParType(String parType) {
        this.parType = parType;
    }

    public String getParOwner() {
        return parOwner;
    }

    public void setParOwner(String parOwner) {
        this.parOwner = parOwner;
    }

    public String getParOwnerContact() {
        return parOwnerContact;
    }

    public void setParOwnerContact(String parOwnerContact) {
        this.parOwnerContact = parOwnerContact;
    }
}

活动代码(其中值传递给PARTYMODEL类的SETTER)在此活动中,我将PARTYMODEL对象添加到数据库中,当值取自硬编码字符串时,它工作正常,当值取自editText时,值未设置:

package com.example.accounts;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddNewParty extends AppCompatActivity {

    EditText etPartyName,etPartyType,etPartyOwner,etPartyOwnerContact;
    Button btnCreate;

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


        etPartyName=findViewById(R.id.et2PartyName);
        etPartyType=findViewById(R.id.et2PartyType);
        etPartyOwner=findViewById(R.id.et2PartyOwner);
        etPartyOwnerContact=findViewById(R.id.et2OwnerContact);
        btnCreate=findViewById(R.id.btn2Create);

        MyDbHandler db = new MyDbHandler(AddNewParty.this);

        PartyModel partyModel = new PartyModel();

        partyModel.setParName(etPartyName.getText().toString());
        partyModel.setParType("Trader");
        partyModel.setParOwner("Mr. XYZ");
        partyModel.setParOwnerContact("9810454563");


        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.addParty(partyModel);
                Intent intent = new Intent(AddNewParty.this,com.example.accounts.MainActivity.class);
                startActivity(intent);
            }`
        });
     }
}

单击按钮后,从editText获取数据

 btnCreate.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
        //if edit texts are not empty ,set it in model class
        if(ePartyName.getText().toString().isEmpty() || 
         netPartyType.getText().toString().isEmpty()|| 
         etPartyOwner.getText().toString().isEmpty() || 
         etPartyOwnerContact.getText().toString().isEmpty())
         return;
    
    else{
         PartyModel partyModel = new PartyModel();
        
           partyModel.setParName(etPartyName.getText().toString());
           partyModel.setParType( netPartyType.getText().toString());
           partyModel.setParOwner(etPartyOwner.getText().toString());                
           
           partyModel.setParOwnerContact(etPartyOwnerContact.getText().toString());
        
        
           db.addParty(partyModel);
         Intent intent = new Intent(AddNewParty.this,com.example.accounts.MainActivity.class);            
                        startActivity(intent);
                    }`}
                });
             }

“成功了吗?”NikhilGuptaYes,成功了。谢谢兄弟。