I am making an app in Android Studio and I am having some troubles with some methods, sorry if the code is bad, I am a CS student and making something like this for the first time.
- Delete method with error
I want to make a method to delete a chosen row and update the view of the database table. The method I have, is only deleting it, but with errors. By errors I mean: whenever I click the row I want to delete and then click on the delete button, the app is restarts. And it actually deletes the row from the db. - Edit (can't find a way to do it)
This edit method should be, something like the INSERT method I already have, fully working I think, but it get's the data from one specific chosen row, and open the activity so I can edit whatever I want;
Here is some code for the methods I already have
//this is my onCreate method, if you need it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
oAPABD = new AjudanteParaAbrirBD(this);
oSQLiteDB = oAPABD.getWritableDatabase();
LinearLayout oLL = (LinearLayout)findViewById(R.id.llsv);
Cursor oCursor = oSQLiteDB.query(
oAPABD.TABLE_NAME,
new String[]{"*"},
null,
null,
null,
null,
null,
null);
boolean bCarryOn = oCursor.moveToFirst();
while (bCarryOn){
LinearLayout oLL1 = (LinearLayout)getLayoutInflater().inflate(R.layout.lines1,null);
oLL1.setId(oCursor.getInt(0)*10+8);
TextView oED1 = (TextView) oLL1.findViewById(R.id.ED1);
TextView oED2 = (TextView) oLL1.findViewById(R.id.ED2);
TextView oED3 = (TextView) oLL1.findViewById(R.id.ED3);
oED1.setId(oCursor.getInt(0)*10+2);
oED1.setText(oCursor.getInt(0)+"");
//COL 2 = ITENS
//DESCRIPTION OF COL2 ITEM LIST MISSING
oED2.setId(oCursor.getInt(0)*10+3);
oED2.setText(oCursor.getInt(2)+"");
oED3.setId(oCursor.getInt(0)*10+4); //repetido 10+4;
oED3.setText(oCursor.getString(3));
oLL.addView(oLL1);
bCarryOn = oCursor.moveToNext();
}
}
//INSERT NEW method
public void novoPedido(View v){
Intent intento1 = new Intent(Main2Activity.this, activityPedido.class );
intento1.putExtra("acao","novoPedido");
intento1.putExtra("count",(getLastID()+1)+"");
intento1.putExtra("currentTime",currentTime);
startActivityForResult(intento1, iRequest_Code1);
}
//EDIT method
public void editPedido(View v){
Intent intento2 = new Intent(Main2Activity.this, activityPedido.class );
intento2.putExtra("acao","editarPedido");
//not implemented
startActivityForResult(intento2,iRequest_Code2);
}
//DELETE method
public void deletePedido(View v){
//oSQLiteDB.delete(oAPABD.TABLE_NAME, "id = ?" + choosenRowID/10,null);
oSQLiteDB.execSQL("DELETE FROM " + oAPABD.TABLE_NAME + " WHERE " + oAPABD.COL1 + "=\"" + choosenRowID/10 + "\"");
oSQLiteDB.close();
LinearLayout oLL1 = (LinearLayout)findViewById(choosenRowID+8);
((LinearLayout) oLL1.getParent()).removeView(oLL1);
}
//decided to go with activityResult like this (only inser method included)
protected void onActivityResult(int iReqCode, int iResultCode, Intent iResult){
oAPABD = new AjudanteParaAbrirBD(this);
oSQLiteDB = oAPABD.getWritableDatabase();
if( (iReqCode == iRequest_Code1) && (iResultCode == RESULT_OK)){ //ADICIONAR
String id = iResult.getStringExtra("id");
String listaItens = iResult.getStringExtra("itens");
String mesa = iResult.getStringExtra("mesa");
String cliente = iResult.getStringExtra("cliente");
LastID = Integer.parseInt(id);
ContentValues oCV = new ContentValues();
oCV.put(oAPABD.COL1, id );
//oCV.put(oAPABD.COL2, listaItens);
oCV.put(oAPABD.COL3, mesa);
oCV.put(oAPABD.COL4, cliente);
oSQLiteDB.insert(oAPABD.TABLE_NAME,null,oCV);
//Toast.makeText(this, "Pedido adicionado com sucesso!", Toast.LENGTH_SHORT).show();
}else if((iReqCode == iRequest_Code2) && (iResultCode == RESULT_OK)){ //EDITAR
}else if((iReqCode == iRequest_Code3) && (iResultCode == RESULT_OK)){ //REMOVER
//Toast.makeText(this, "Pedido removido com sucesso!", Toast.LENGTH_SHORT).show();
}else{
//Toast.makeText(this, "Ups! Algo correu mal...", Toast.LENGTH_SHORT).show();
}
}
on my other activity, the one which I'm using for inserting and then it gives me the result back is defined like this:
public class activityPedido extends Activity {
AjudanteParaAbrirBD oAPABD;
SQLiteDatabase oSQLiteDB;
TextView tvAction;
EditText etInfo1,etInfo2,etInfo3,etCliente,etEmpregado,etPedido;
ImageButton bSaveNew,bSaveEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedido);
Intent iCameFromAct2 = getIntent();
String action = iCameFromAct2.getStringExtra("acao");
String currentTime = iCameFromAct2.getStringExtra("currentTime");
tvAction = (TextView)findViewById(R.id.tAction);
etInfo1 = (EditText)findViewById(R.id.etInfo1);
bSaveNew = (ImageButton)findViewById(R.id.bSaveNew);
bSaveEdit = (ImageButton)findViewById(R.id.bSaveEdit);
etInfo3 = (EditText)findViewById(R.id.etInfo3);
etPedido = (EditText)findViewById(R.id.etPedido);
if (action.equals("novoPedido")){
tvAction.setText("Novo Pedido");
etInfo3.setText(currentTime);
bSaveEdit.setVisibility(View.GONE);
etInfo1.setText(iCameFromAct2.getStringExtra("count"));
etInfo1.setInputType(InputType.TYPE_NULL);
}
if (action.equals("editarPedido")){
tvAction.setText("Editar Pedido");
bSaveNew.setVisibility(View.GONE);
}
if (action.equals("removerPedido")){
tvAction.setText("Remover Pedido");
}
}//onCreate
/*Using this to make the "same button" with 2 different onClick methods:
saveNew(save data when clicked ADD NEW in the previous activity)
and saveEdit(bellow)
which I haven't yet done anything to it and is the one I really needed help,
because I can't figure out out to make it)*/
public void saveNew(View v){
etInfo1 = (EditText)findViewById(R.id.etInfo1);
etInfo2 = (EditText)findViewById(R.id.etInfo2);
etCliente = (EditText)findViewById(R.id.etCliente);
etPedido = (EditText)findViewById(R.id.etPedido);
Intent iResult = new Intent();
iResult.putExtra("id",etInfo1.getText().toString());
iResult.putExtra("mesa",etInfo2.getText().toString());
iResult.putExtra("cliente",etCliente.getText().toString());
iResult.putExtra("itens",etPedido.getText().toString());
setResult(RESULT_OK, iResult);
super.finish();
}
public void saveEdit(View v){
etInfo1 = (EditText)findViewById(R.id.etInfo1);
etInfo2 = (EditText)findViewById(R.id.etInfo2);
etCliente = (EditText)findViewById(R.id.etCliente);
etPedido = (EditText)findViewById(R.id.etPedido);
super.finish();
}
public void goBack(View v){
finish();
}
}
Adding the error in LogCat as asked in the comments:
2019-11-20 14:31:10.355 14923-14923/pmd.di.ubi.pedidunchos_2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: pmd.di.ubi.pedidunchos_2, PID: 14923
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5652)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6592)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25951)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6815)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5647)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6592)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25951)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6815)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.widget.LinearLayout.getParent()' on a null object reference
at pmd.di.ubi.pedidunchos_2.Main2Activity.deletePedido(Main2Activity.java:134)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5647)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6592)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25951)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6815)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2019-11-20 14:31:10.437 1371-1830/? E/InputDispatcher: channel '55262fd pmd.di.ubi.pedidunchos_2/pmd.di.ubi.pedidunchos_2.Main2Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-11-20 14:31:10.450 1371-1830/? E/InputDispatcher: channel 'c67c606 pmd.di.ubi.pedidunchos_2/pmd.di.ubi.pedidunchos_2.MainActivity (server)' ~
Channel is unrecoverably broken and will be disposed!