-5

It's been a month since I've kicked off with android studio, at this time I'm stuck at a point where I have to pass a set of <EditText/> values from MainActivity to UpdateActivity and then send those values to the Sqlite database for updating a table.

What I have accomplished:

Successfully sending values from MainActivity's <EditText/> to UpdateActivity's <EditText/>

How I have done that from MainActivity:

public class MainActivity extends AppCompatActivity {
    DatabaseHelper myDb;
    EditText editName, editSurname, editMarks;
    Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);

    editName = (EditText) findViewById(R.id.UpdName);
    editSurname = (EditText) findViewById(R.id.editTextSurname);
    editMarks = (EditText) findViewById(R.id.UpdMarks);
    btnUpdate = (Button)findViewById(R.id.btnUpdate);

    btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String MainName = editName.getText().toString();
            String MainSurname = editSurname.getText().toString();
            String MainMarks = editMarks.getText().toString();

            Intent UpdateAct = new Intent(MainActivity.this, UpdateActivity.class);
            UpdateAct.putExtra("Name", MainName);
            UpdateAct.putExtra("SurName",MainSurname );
            UpdateAct.putExtra("Marks", MainMarks);
            startActivity(UpdateAct);
        }
    });
}

Then inside the UpdateActivity:

public class UpdateActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button btnUpd;
EditText editTextId, editUpdtName, editUpdSurname, editUpdMarks;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update);

    Intent getData = getIntent();
    String Name = getData.getStringExtra("Name");
    String SurName = getData.getStringExtra("SurName");
    String Marks = getData.getStringExtra("Marks");

    editTextId = (EditText)findViewById(R.id.editTextID);
    editUpdtName = (EditText)findViewById(R.id.UpdName);
    editUpdSurname = (EditText)findViewById(R.id.UpdSurname);
    editUpdMarks = (EditText)findViewById(R.id.UpdMarks);
    btnUpd = (Button)findViewById(R.Id.btnUpd);

    editUpdtName.setText(Name);
    editUpdSurname.setText(SurName);
    editUpdMarks.setText(Marks);
}

What I'm stuck at is to send these values to a function named updateData(); For that I have tried to use the values from MainActivity directly to UpdateActivity but failed, then I tried to cast those <EditText/> and use it inside the function but failed again.

Inside my UpdateActivity:

..//    
        editUpdtName.setText(Name);
        editUpdSurname.setText(SurName);
        editUpdMarks.setText(Marks);
        //updateData(Name, SurName, Marks);
        updateData();
    }

//function definiton
//   public void updateData(final String Name, final String SurName, final String Marks)
    public void updateData()
    {
        btnUpd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//             boolean isUpdated =   myDb.updateData(editTextId.getText().toString(), Name,SurName, Marks);
                boolean isUpdated =   myDb.updateData(editTextId.getText().toString(), editUpdtName.getText().toString(),
                        editUpdSurname.getText().toString(), editUpdMarks.getText().toString());
            if(isUpdated == true){
                Toast.makeText(UpdateActivity.this, "Data Updated Successfully!", Toast.LENGTH_SHORT).show();
            }
            else{
                    Toast.makeText(UpdateActivity.this, "Data NOT Updated!", Toast.LENGTH_SHORT).show();
        }

    }
        });
    }
}

The app crashes everytime the function updateData(); is called, to ensure that is the real cause I have even commented and also tried putting it in a handler to run it after 3000ms, as expcted it crashed after the time period.

Solved it by initializing the db.

myDb = new DatabaseHelper(this);

Zoe
  • 27,060
  • 21
  • 118
  • 148
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • Duplicate of https://stackoverflow.com/q/218384/6296561 and https://stackoverflow.com/q/23353173/6296561 – Zoe Apr 19 '19 at 20:42

3 Answers3

1

You forgot to initialize myDb. Initialize it in your onCreate() method:

myDb = new DatabaseHelper(this);
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • Okay. And what about `myDb`? Do you initialize it? – Prerak Sola Aug 17 '16 at 09:56
  • I cannot believe I missed that! I did like a tons of other things to fixed it but ignored the minor little fact of Initializing the `myDb` -.- – DirtyBit Aug 17 '16 at 09:59
  • Happens! Happy coding! – Prerak Sola Aug 17 '16 at 10:03
  • As per my experience, those are for **missing logcat** in the question. *My app crashes*, doesn't give anything to find a solution, so always post the error logcat as it helps in figuring out what the exact error is. – Prerak Sola Aug 17 '16 at 10:07
  • I was going to add logcat but solved it by intializig it already so I did not! Any ways thank you for the help man :) – DirtyBit Aug 17 '16 at 10:08
0

Why not just pass the values to updateData()?

editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
updateData(Name, SurName, Marks);
Egor
  • 39,695
  • 10
  • 113
  • 130
0

have you initialized btnUpd? I can not see this in your onCreate()

btnUpd = (Button) findViewById(...)
Chris623
  • 2,464
  • 1
  • 22
  • 30