Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm extremely beginner to android development, I'm making a simple app that has a registration form, but after the fields filled with data and when the save button pressed the app closed immediately. I've included the code because I don't know where's the faulty part:

Java
public void AddNew(View v)
  {
      if(Image_path.isEmpty())
      {
          Toast.makeText(this,"please choose a profile picture",Toast.LENGTH_SHORT).show();
          return;
      }

      EditText ed_full_name = findViewById(R.id.full_name);
      EditText ed_mobie = findViewById(R.id.phone);
      EditText ed_id_num = findViewById(R.id.id_num);
      EditText ed_address = findViewById(R.id.address);
      EditText ed_section_id = findViewById(R.id.straction_id);
      EditText ed_card_id = findViewById(R.id.card_id);
      EditText ed_dis_id = findViewById(R.id.dis_id);
      String full_name = ed_full_name.getText().toString();
      String mobile = ed_mobie.getText().toString();
      String id_num = ed_id_num.getText().toString();
      String address = ed_address.getText().toString();
      String card_id = ed_card_id.getText().toString();
      String section_id = ed_section_id.getText().toString();
      String dis_id = ed_dis_id.getText().toString();
      if(full_name.isEmpty() || mobile.isEmpty() || id_num.isEmpty() || address.isEmpty() || card_id.isEmpty() || section_id.isEmpty())
      {
          Toast.makeText(this,"Please fill all fields",Toast.LENGTH_SHORT).show();
          return;
      }
      DBHelper mHelper = new DBHelper(this);
      SQLiteDatabase db = mHelper.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put("full_name", full_name);
      values.put("mobile", mobile);
      values.put("id_num", id_num);
      values.put("address", address);
       values.put("section_id", section_id);
      values.put("card_id", card_id);
      values.put("image_path", Image_path);
      values.put("dis_id", dis_id);
      db.insertWithOnConflict("Clients",
              null,
              values,
              SQLiteDatabase.CONFLICT_REPLACE);
      db.close();
      Toast.makeText(this, "Saved successfully! ", Toast.LENGTH_SHORT).show();
      finish();


What I have tried:

I inspected the code with no result, as I mentioned I'm really biginner to find out what the problem, i hope you would help.
Posted
Updated 4-Jun-20 4:33am
Comments
JudyL_MD 4-Jun-20 8:44am    
In addition to what Patrice said ... the debugger in Android Studio is pretty good. Also, look in your LogCat output; find and click the tab along the bottom of Android Studio that says LogCat. There should be entries reporting what exception was thrown that crashed your app. Look for the lines in red (the default color for the ERROR output), and it will show you the call stack. It will even allow you to click on the line and take you right to that spot in the source code that generated the exception.

Quote:
I don't know where's the faulty part

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.


Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
[Update]
Quote:
I wonder if you call this an answer or a solution.. I also wonder if you read the post, tags or at least the title..
I read memes about posting a code on Codeproject as a beginner and how some people eat him a live!! Now I understand.

I started my post about being a extremely beginner which means a did my best.
I hope that I got a useful hint or help instead of blaming or self flagellation..

In practice, we have no way to reproduce your problem as it depend on your configuration and probably on your database.
The debugger will help you to spot the position of crash and get context. And if no crash, it will show you exactly what is doing your code.
 
Share this answer
 
v2
Comments
M.S.S.E 4-Jun-20 9:13am    
Hi sir,
I wonder if you call this an answer or a solution.. I also wonder if you read the post, tags or at least the title..
I read memes about posting a code on Codeproject as a beginner and how some people eat him a live!! Now I understand.

I started my post about being a extremely beginner which means a did my best.
I hope that I got a useful hint or help instead of blaming or self flagellation..

just remember your self when you printed you Hello world!.

I hope that you ignore this post.
Thank you.
David Crow 6-Jun-20 10:32am    
See my misplaced comment below.
Patrice T 5-Jun-20 17:04pm    
Hi David,
Thanks for this comment, but I think you should have used the 'reply' button on top of comment, so OP is notified.
Look at your code. The last thing you do in the AddNew method is to call finish()[^] which terminates your activity. The last thing you should do in any non-main method is return to where you were called from.
 
Share this answer
 
Comments
M.S.S.E 4-Jun-20 17:53pm    
I replaced
finish
with
return
but the same problem occured.
Richard MacCutchan 5-Jun-20 3:15am    
OK, so that may not be the issue. So you you need to get the debugger set up to trace through the code to find out exactly where it is crashing. It could be in the code that handles the Save button, so please update the question and show us that code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900