Welcome to part 14 of my Android Development tutorial! In this part of the tutorial I will completely finish the Android Address Book App. I will also spend a lot of time answering questions I recently received on Contexts, Intents, how to pass data between Activities using putExtra() and and getStringExtra(). I’ll also cover how to import Android packages into Eclipse.
Here is the final complete Android Address Book App Package. All of the specific code I worked with in this tutorial can be found below.
If you like tutorials like this, it helps to tell Google+ with a click here
Code From the Video
EditContact.java
// EditContact.java package com.newthinktank.contactsapp; import java.util.HashMap; import android.os.Bundle; import android.widget.EditText; import android.app.Activity; import android.content.Intent; import android.view.View; public class EditContact extends Activity{ // Allows access to data in the EditTexts EditText firstName; EditText lastName; EditText phoneNumber; EditText emailAddress; EditText homeAddress; // The database tool class DBTools dbTools = new DBTools(this); // Sets up everything when the Activity is displayed public void onCreate(Bundle savedInstanceState) { // Get saved data if there is any super.onCreate(savedInstanceState); // Designate that edit_contact.xml is the interface used setContentView(R.layout.edit_contact); // Get the EditText objects firstName = (EditText) findViewById(R.id.firstName); lastName = (EditText) findViewById(R.id.lastName); phoneNumber = (EditText) findViewById(R.id.phoneNumber); emailAddress = (EditText) findViewById(R.id.emailAddress); homeAddress = (EditText) findViewById(R.id.homeAddress); // Intent defines that an operation will be performed Intent theIntent = getIntent(); // Get the extended data provided to this activity // putExtra("contactId", contactIdValue); in MainActivity // will pass contactId here String contactId = theIntent.getStringExtra("contactId"); // Get the HashMap of data associated with the contactId HashMap<String, String> contactList = dbTools.getContactInfo(contactId); // Make sure there is something in the contactList if(contactList.size()!=0) { // Put the values in the EditText boxes firstName.setText(contactList.get("firstName")); lastName.setText(contactList.get("lastName")); phoneNumber.setText(contactList.get("phoneNumber")); emailAddress.setText(contactList.get("emailAddress")); homeAddress.setText(contactList.get("homeAddress")); } } public void editContact(View view) { HashMap<String, String> queryValuesMap = new HashMap<String, String>(); // Get the EditText objects firstName = (EditText) findViewById(R.id.firstName); lastName = (EditText) findViewById(R.id.lastName); phoneNumber = (EditText) findViewById(R.id.phoneNumber); emailAddress = (EditText) findViewById(R.id.emailAddress); homeAddress = (EditText) findViewById(R.id.homeAddress); // Intent defines that an operation will be performed Intent theIntent = getIntent(); // Get the extended data provided to this activity // putExtra("contactId", contactIdValue); in MainActivity // will pass contactId here String contactId = theIntent.getStringExtra("contactId"); // Put the values in the EditTexts in the HashMap queryValuesMap.put("contactId", contactId); queryValuesMap.put("firstName", firstName.getText().toString()); queryValuesMap.put("lastName", lastName.getText().toString()); queryValuesMap.put("phoneNumber", phoneNumber.getText().toString()); queryValuesMap.put("emailAddress", emailAddress.getText().toString()); queryValuesMap.put("homeAddress", homeAddress.getText().toString()); // Send the HashMap to update the data in the database dbTools.updateContact(queryValuesMap); // Call for MainActivity this.callMainActivity(view); } public void removeContact(View view) { Intent theIntent = getIntent(); String contactId = theIntent.getStringExtra("contactId"); // Call for the contact with the contactId provided // to be deleted dbTools.deleteContact(contactId); // Call for MainActivity this.callMainActivity(view); } // Calls for a switch to the MainActivity public void callMainActivity(View view) { // getApplication returns an Application object which allows // you to manage your application and respond to different actions. // It returns an Application object which extends Context. // A Context provides information on the environment your application // is currently running in. It provides services like how tp obtain // access to a database and preferences. // Google says a Context is an entity that represents various // environment data. It provides access to local files, databases, // class loaders associated to the environment, services including // system-level services, and more. // The following Intent states that you want to switch to a new // Activity being the MainActivity Intent objIntent = new Intent(getApplication(), MainActivity.class); startActivity(objIntent); } }
In NewContact.java at line 56 it must be called “phoneNumber” instead of “pnoneNumber”
Ill check that. Sorry about the error
Hi Derek
I saw someone asking you to do a UI like Feedly readerβs UI. The only thing exotic on Feedly reader UI is its sliding menu part. And I am interested in that too. I wonder if you could implement a sliding menu tutorial. Also, you have done xlm parsing. Can you do a JSON parsing app too. Thanks.
Mike
I’m going to cover fancy menus and much more. Sure I can cover JSON parsing. I should have done it in the previous tutorial. I may just add it on now. I don’t know if that would confuse people or not?
Thank you. Let me say it one more time. I do appreciate your efforts and I am forever grateful.
Thank you for being a part of my awesome community here π I appreciate it!
Thank u so much for the excellent tutorials.
There is an issue in callMainActivity. After u add a new contact, it does not go back to the existing MainActivity. It always creates a new MainActivity instance. If u press the Back button, it goes back to NewContact.
I suggest getting the result of NewContact activity and then refreshing MainActivity. http://developer.android.com/training/basics/intents/result.html
You are very correct sir! Sorry about that. I need to hire you to catch my little bugs π You are very talented and have an excellent eye!
Vince,
Could you sent me the solution with regards to the
issue in the callMainActivity ?
Thank you,
Anton
Derek,
Could you send me the solution please.
Please tell me exactly what you need and I’ll provide it
Hi Derek. I have been watching your Android tutorials since you released your first one. Since then I have been hooked on developing for Android and I have released 3 apps. The first one is a simple app for tracking your income and expenses, and gives an overview in the form of a pie chart and a bar chart. The other two is quiz apps, flag quiz and soccer quiz. Although I’m mainly developing for fun and experience it would be cool if someone actually downloaded my apps. Do you have any marketing tips to get people to notice your apps? Be it Play Store search rankings or other tricks. I know my apps isn’t exactly the only of its kind, but I’m certain I would get more downloads if people actually noticed my apps. Being on the 20th page of search doesn’t exactly help me get attention.
Wow, I’m happy that the videos have helped that much! Great Job! As per marketing the apps I’d say you’re better off trying to make a YouTube video about them using keyword tricks. I mainly make Android apps for private companies so I have never had to market on the Android marketplace.
Try using the Google keyword tool to find keywords that are in high demand, but have a low level of competition.
I went there and typed in the following
android expense tracker app
best expense tracker app for android
spending tracker app android
Based off the results a YouTube video on your app named “on track app for android” might work if your app is named On Track?
I think YouTube is the best way to market for free online. It is very hard to use keywords to drive traffic anymore to a regular site. As more people start using YouTube to market seriously though that will probably no longer be true.
I hope that helps
Thanks. That tool is actually a really good resource. I’ll definitely look into uploading some videos on Youtube.
You should definitely do that!
Is there anything that I need to change in Manifest? Thank you for tutorials…
I provide all of the code in a complete package that you can download. I show everything that needs to be changed over the course of the tutorials. Sorry if they were confusing. I’ll do my best to improve π
Hi Derek,
sorry to bother you with a question, but I’m having an issue with an application that I’m doing in android.
the thing is that the application was coded and is working on my smartphone (android 4.1.2) and other devices with this version of android.
the thing is that when I try it on a device with android 2.3.7, the application fails when I’m going to the second activity.
I debugged the application, and it fails on the “onCreate” method (Screenshot: http://screencast.com/t/Kl5CsMdptN7F) the error is happening in the selected line)
the strange thing is that a similar method is being used for inflating the previous activity, but it seems that is working fine there…
if it works, the manifest for the app is set to Min SDK Version = 8.
please let me know if this information is enough, or if you want me to share my project folder for you to check.
I’m sorry if I’m going to far with this, but I tried to google how to do this and I’m not getting anywhere.
thank you and my best regards. I appreciate a lot what you do for us newbies.
GastΓ³n-
Hi Derek, I wanted to say that I fixed my error. thank you!
Great I’m glad to hear that π
Hi GastΓ³n
Do you have a layout xml file named activity_uniflow_results.xml?
Without one you would get an error, but if you do have one I don’t think that line is were your error lies.
I hope that helps
Derek
Yes, I have the .XML file, and the error was there, I was using a “space” element in my GUI, and I found out that this method was implemented in the 14 SDK version, hence it was not working with android 2.3 (SDK 8). I changed the Space for a View and it started working π
Great! I’m glad you fixed it π
Receiving error “addressbookapp has stopped unexpectedly “. I ran your contactsapp on the same avd using eclipse but in your case its working fine .What am I doing wrong?
You dint mention changing the android manifest file but I changed that as well.All the code and layouts are entirely same.
What error are you seeing in the LogCat panel in Eclipse?
Hi Derek,
Good Day to you!
I have also followed your tutorials from Tutorial 10 to Tutorial 14. I am able to run the addressbookapp but after I click on the “ADD” button on the “Contact List” page, the app also stopped unexpectedly with the follow error message “Unfortunately, Contact List has stopped.”
Below is the info from the LogCat panel:
08-12 01:28:45.745: E/Trace(873): error opening trace file: No such file or directory (2)
08-12 01:28:46.305: D/dalvikvm(873): GC_CONCURRENT freed 43K, 7% free 2777K/2964K, paused 13ms+11ms, total 77ms
08-12 01:28:46.496: D/gralloc_goldfish(873): Emulator without GPU emulation detected.
08-12 01:28:48.075: D/AndroidRuntime(873): Shutting down VM
08-12 01:28:48.075: W/dalvikvm(873): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-12 01:28:48.115: E/AndroidRuntime(873): FATAL EXCEPTION: main
08-12 01:28:48.115: E/AndroidRuntime(873): java.lang.IllegalStateException: Could not execute method of the activity
08-12 01:28:48.115: E/AndroidRuntime(873): at android.view.View$1.onClick(View.java:3599)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.view.View.performClick(View.java:4204)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.view.View$PerformClick.run(View.java:17355)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.os.Handler.handleCallback(Handler.java:725)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.os.Looper.loop(Looper.java:137)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-12 01:28:48.115: E/AndroidRuntime(873): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 01:28:48.115: E/AndroidRuntime(873): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 01:28:48.115: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-12 01:28:48.115: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-12 01:28:48.115: E/AndroidRuntime(873): at dalvik.system.NativeStart.main(Native Method)
08-12 01:28:48.115: E/AndroidRuntime(873): Caused by: java.lang.reflect.InvocationTargetException
08-12 01:28:48.115: E/AndroidRuntime(873): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 01:28:48.115: E/AndroidRuntime(873): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.view.View$1.onClick(View.java:3594)
08-12 01:28:48.115: E/AndroidRuntime(873): … 11 more
08-12 01:28:48.115: E/AndroidRuntime(873): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.newthinkjuly.addressbookapp/com.newthinkjuly.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml?
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Activity.startActivityForResult(Activity.java:3370)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Activity.startActivityForResult(Activity.java:3331)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Activity.startActivity(Activity.java:3566)
08-12 01:28:48.115: E/AndroidRuntime(873): at android.app.Activity.startActivity(Activity.java:3534)
08-12 01:28:48.115: E/AndroidRuntime(873): at com.newthinkjuly.addressbookapp.MainActivity.showAddContact(MainActivity.java:72)
08-12 01:28:48.115: E/AndroidRuntime(873): … 14 more
Just wonder, do you have any idea or suggestions on how I should go about in resolving this error and have the app running?
Feel free to let me know if there are any other info you may require?
Thank you
Best Regards,
Chen
Hi Chen,
When you look at the LogCat, always look for your classes in the errors. A lot of junk errors are showing there. Based on what you sent, look here for errors
08-12 01:28:48.115: E/AndroidRuntime(873): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.newthinkjuly.addressbookapp/com.newthinkjuly.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml?
08-12 01:28:48.115: E/AndroidRuntime(873): at com.newthinkjuly.addressbookapp.MainActivity.showAddContact(MainActivity.java:72)
I also have working code in package form that I’m sure works. I hope that helps
Derek
I was also having that problem. To fix, make sure to include the EditContact and NewContact activities in the manifest file:
I want to develope an app in android for college campus …
I dnt hv any idea hw can I connect my server..
Bcoz der will be dynamic update will br there in my app..
So I hv no.idea hw cn I do that..
CAn u pls give me.the idea hw can I do dynamic update in.my app..
Look into BroadcastReceivers. I’ll cover them soon
Hello Derek..
Droiddraw is one app to draw the UI..
Can we use that app to draw our UI for our android app..
And what is the adv and dis adv of this app..??
Hello, Droiddraw is in Beta and I haven’t found it to be worth covering. The main reason why is that it may or may not receive the support required to work with future versions of Android. I personally avoid using anything except those tools specifically recommended by Google. It is very common for tools like this to break and the support for them is most always very poor. This is just a personal preference and I know others who prefer to always use the newest tools. I don’t because I’ve been burned to many times. I hope that helps explain my position.
Thank you so much..
You’re very welcome π
I want to create AlarmManager and I have question. Will this work if I add not name or number or email, but I will put there timePicker and datePicker.
Derek,
Excellent tutorials – great job.
As Vince mentioned earlier addressbook app: “There is an issue in callMainActivity. After u add a new contact, it does not go back to the existing MainActivity. It always creates a new MainActivity instance. If u press the Back button, it goes back to NewContact.
I suggest getting the result of NewContact activity and then refreshing MainActivity.”.
Could you please show how this is accomplished?
Thank you – keep up the great work!
Thank you very much π It is pretty easy to refresh an activity. You just need these 2 lines of code
finish();
startActivity(getIntent());
I hope that helps. I’ll try to fit that into a tutorial
Instead of declaring contactId and the EditText objects in each method, does it work to just make them instance variables and assign them in onCreate()?
Yes you could do that. I often write everything out the long way to help people understand what is going on at each step
Hi Derek,thank you so much for those tutorials,can you please help me about add blocked contact between two time slots in any day of the week ,I hope that helps.
Sorry but I’m not sure what you are trying to do.
Hi Derek, first thank you for the tutorials, they’re quite helpful and you’re a very good teacher xD
I’m stuck in this problem that stop my application everytime I click on the Contact Name. Here is the LogCat.
First I thought this were a problem on xml file, but I called the edit_ficha.xml (same as edit_contact.xml) in other function and it was totally normal. But I’m thinking that might be a problem in the EditFicha.class but i don’t know what problem could be, the class is the same as your EditContact.class :/
If you could help I would be very grateful o/
Ops, problems on the blockquote xD
here the Logcat
09-12 19:14:46.025: E/AndroidRuntime(663): FATAL EXCEPTION: main
09-12 19:14:46.025: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.labpet.petapp/com.labpet.petapp.EditFicha}: java.lang.NullPointerException
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:137)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-12 19:14:46.025: E/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 19:14:46.025: E/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 19:14:46.025: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-12 19:14:46.025: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-12 19:14:46.025: E/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method)
09-12 19:14:46.025: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException
09-12 19:14:46.025: E/AndroidRuntime(663): at com.labpet.petapp.EditFicha.onCreate(EditFicha.java:41)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.Activity.performCreate(Activity.java:5008)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-12 19:14:46.025: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-12 19:14:46.025: E/AndroidRuntime(663): … 11 more
09-12 19:14:48.115: E/Trace(678): error opening trace file: No such file or directory (2)
I looked through the errors and I don’t see any errors specific to the classes created. Try downloading my whole finished package and run that code to see what happens.
Tell me if you have any other problems.
Hi.. can you provided the code for the androidmanifest.xml file?
On this page I have a link to the whole finished package which has the manifest file in it. Here is a link to the package.
I Derek …I see nothing of the manifest?!?
The Manifest file is in the package for the app. Here is the package
Hello Derek,
How about using startActivityForResult() rather than startactivity();
Is it possible.
I just wanted to know how startActivityForResult() different from startactivity();
Umair
Hi Umair,
You use startactivity() when you simply want to start an activity and nothing else.
Use startActivityForResult() when you start an activity just to have it do something and then end. Take a picture, get input from the user, etc.
Hey Derek!
Thank you so much for all of your videos. I find them so helpful as I start this android experience.
I was wondering how to create a search bar to search for a contact. Thank you
Hey James,
You’re very welcome π Basically you just need to put an EditText box in the app that excepts input. Then when it triggers an event handler use that input in the query. Give it a try and I’m sure you’ll get it. Everything you need has been covered.
Hi derek, my apologies to bother you and whatnot but the code that you put in the .zip file actually does not work when imported into Eclipse. Are you aware of any such malfunctions?
When I first import the file, it comes up with hundreds of Build Path errors, if I use the option to ‘Fix Project Automatically’ (Eclipse) I get a persistent [2013-10-29 11:22:13 – ContactsApp] Unable to resolve target ‘android-17’
I’d make sure you have Eclipse set up properly because I’m not getting any errors. How are you importing the code and what version of Eclipse are you using?
What error are you getting?
Hi Derek, I am having the problem of
11-05 08:08:39.767: E/AndroidRuntime(21131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.image/com.example.image.List_admin}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘android.R.id.list’
when I am going to show all the data of my table into ListView.
Do you know actually what is the mistake that i did ? thx
Try changing the list id in the layout file to android:id=”@android:id/list”
sir Derek , can you help me with this because if I run the app , the emulator will show a pop up box that says “application unfortunately stopped” and there’s an error that says
11-05 08:08:39.767: E/AndroidRuntime(21131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.image/com.example.image.List_admin}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is βandroid.R.id.listβ
but in my xml file I already have this ID .
android:id=”@+id/list”
but still .
it will just stop .
can you please help me with this ?
THANKS π
Try using android:id=”@android:id/list”
Hello Mr. Banas , I have a problem with my address book .
When I press the “ADD” button my address book will suddenly stopped .
11-15 20:39:14.820: E/AndroidRuntime(1047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 20:39:14.820: E/AndroidRuntime(1047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-15 20:39:14.820: E/AndroidRuntime(1047): at dalvik.system.NativeStart.main(Native Method)
11-15 20:39:14.820: E/AndroidRuntime(1047): Caused by: java.lang.reflect.InvocationTargetException
11-15 20:39:14.820: E/AndroidRuntime(1047): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 20:39:14.820: E/AndroidRuntime(1047): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.view.View$1.onClick(View.java:3809)
11-15 20:39:14.820: E/AndroidRuntime(1047): … 11 more
11-15 20:39:14.820: E/AndroidRuntime(1047): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.vinceport.addressbookapp/com.vinceport.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml?
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Activity.startActivityForResult(Activity.java:3423)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Activity.startActivityForResult(Activity.java:3384)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Activity.startActivity(Activity.java:3626)
11-15 20:39:14.820: E/AndroidRuntime(1047): at android.app.Activity.startActivity(Activity.java:3594)
11-15 20:39:14.820: E/AndroidRuntime(1047): at com.vinceport.addressbookapp.AddressBook.showAddContact(AddressBook.java:71)
11-15 20:39:14.820: E/AndroidRuntime(1047): … 14 more
here’s the LogCat says .
Here is a link the the whole package. Try importing it into Eclipse and running it. That will clear the errors. Then use a website like diffnow.com to compare your class files, layout files and manifest to mine to find the error.
It seems from the log that
11-15 20:39:14.820: E/AndroidRuntime(1047): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.vinceport.addressbookapp/com.vinceport.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml? –
Manifest file is missing the declaration of the other activities created, declare those also there like
EditContact
NewContact
Thank you for your VERY GOOD tutorials Mr. Derek and I really appreciated it :DD
but I have some little problem …
because when I input some records and press the SAVE button .
my address book will suddenly STOP ..
can you please help me with this ?
here the errors . . .
11-18 17:01:18.460: E/AndroidRuntime(872): at android.view.View$1.onClick(View.java:3809)
11-18 17:01:18.460: E/AndroidRuntime(872): … 11 more
11-18 17:01:18.460: E/AndroidRuntime(872): Caused by: java.lang.NullPointerException
11-18 17:01:18.460: E/AndroidRuntime(872): at com.vinceport.addressbookapp.NewContact.addNewContact(NewContact.java:42)
11-18 17:01:18.460: E/AndroidRuntime(872): … 14 more
THANK YOU in advance Mr. Derek π
You’re very welcome π You get that error when something has the value of null when it shouldn’t. You have a typo some place in your code. Compare your class files to mine using something like diff now.com
I hope that helps π
Hi! I wrote on your YouTube video about posting errors. I’ve managed to fix most of the ones myself but I’m just stuck on this one. I have used the diffnow site to try and spot differences between my code and what you have supplied but now I’m struggling to see any! When I add a contact I get taken back to blank screen and errors appear on my LogCat. This doesn’t happen with your code, only with mine! The errors are as follows…
11-30 15:08:06.555: E/SQLiteLog(7504): (1) table contacts has no column named emailAddress
11-30 15:08:06.565: E/SQLiteDatabase(7504): Error inserting emailAddress=joe@email.com lastName=Bloggs phoneNumber=01111111111 firstName=Joe homeAddress=1 test street
11-30 15:08:06.565: E/SQLiteDatabase(7504): android.database.sqlite.SQLiteException: table contacts has no column named emailAddress (code 1): , while compiling: INSERT INTO contacts(emailAddress,lastName,phoneNumber,firstName,homeAddress) VALUES (?,?,?,?,?)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at com.example.youtubeaddressbook.DBTools.insertContact(DBTools.java:43)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at com.example.youtubeaddressbook.NewContact.addNewContact(NewContact.java:47)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at java.lang.reflect.Method.invoke(Method.java:511)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.view.View$1.onClick(View.java:3655)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.view.View.performClick(View.java:4162)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.view.View$PerformClick.run(View.java:17152)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.os.Handler.handleCallback(Handler.java:615)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.os.Looper.loop(Looper.java:137)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at android.app.ActivityThread.main(ActivityThread.java:4867)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at java.lang.reflect.Method.invoke(Method.java:511)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-30 15:08:06.565: E/SQLiteDatabase(7504): at dalvik.system.NativeStart.main(Native Method)
Any tips? I’m baffled!
The emailAddress wasn’t created for some reason in the database. I have the whole package here which you can compare to your code to find the typo. Compare your class files to the ones in the package using a site like diffnow.com and you’ll see the error. I hope that helps π
I retyped emailAddress and then it somehow worked! very bizarre! But thank you π
In the mean time I went through all the tutorials again and made another and with that one I have an issue with the list view..it just shows the first name twice of an added item when displayed then when you go to edit it it crashes and the errors talk about a null pointer on the onCreate?
Sorry for all the questions, I just really want to understand whats going on with all aspects of it!
The best thing to do is to get the class files in my src folder and my layout files and compare yours to mine using something like diffnow.com. It will point out the differences. I hope that helps π
I noticed in the youtube comments that others had the same problem, the manifest file does not seem to update automatically for everyone, maybe this should be mentioned in the video? π
I’m talking about the two activity tags:
Another question. If I want the entries to show up like “lastName, firstName” instead, is there any “pretty” way of doing this?
I changed the getAllContacts method to do
contactMap.put(“lastName”, cursor.getString(2) + “, “);
But this doesn’t feel like “best practice”.
I did everything pretty nice. But when I run the application in my Emulator it shows and error and says forced to close the application, and crashes. Why is that?
What error shows up in the LogCat panel in Eclipse? Try using the included package I provide and see if you get any errors.
Your package works fine..
Actually the Problem was the ‘list view name’..
I missed the ‘@android:…’ format for list view..
Thanks for your great tutorials(even though you are as fast as a metro train! π )
Thank you π Yes I’m working on finding the best speed for the tutorials.
great stuff as always Derek!
Thanks a lot for all of the fantastic tutorials ~
I have tried testing the app on my phone as well as an AVD and get the same result.
I click “ADD” to add a contact and it lets me walk through the fields and click “Save”. But then it does not display the contact I just added. However it adds a record because it allows me to press where the contact name should be and then gives me an error about EditContact. I figure I am getting an error on EditContact because there is no contact to edit!
Any idea on what I may be missing?
Thank you very much π Have you tried testing the final complete package for my Android Address App? Tell me if that doesn’t work. What errors are you seeing?
AHA! I finally got it ~
I tried importing your code but kept getting errors (tried cleaning project everything but couldn’t get it to work) – so I just started a new project and hand created the files and copied pasted and got it to work and run properly, yay!
So then I had to figure out what the differences were in your code and mine. And keep in mind when I say “my code”, that was just your code I copied and pasted from each page of this tutorial so when I mention these differences below, you may want to change them on your tutorial pages if that matters to you π
1) DBTools.java: added @Override before oncreate and onupgrade
2) DBTools.java: added ContactID field on the contactMap.put
3) MainActivity.java: changed theIntent to theIndent (maybe just a typo but I changed it anyways)
4) MainActivity.java: changed getApplication() to getApplicationContext in showAddContact (I thought this for sure was going to fix immediately but I saved and ran it and still the same result)
— Now these next 2 changes are what really made the difference —
5) contact_entry.xml: changed contactName textview into 2 separate ones (firstName and lastName) – when I did this, all my contact names showed up in the list, but
still had error on edit screen
6) edit_contact.xml: changed all editText names from lastNameEditText format to just lastName format (started displaying everything then and working properly!)
(one last side note in NewContact.java you have phoneNumber spelled as pnoneNumber)
Thanks again Derek, after I figured out how to compare files within eclipse it made this process much quicker for me to find what was different)
I leave this comment with 1 question too: I see how you use the SQLite3 db to add and edit records locally but how do you set up a database that anyone in the world can edit and add records too? So for example, take this same app idea and allow multiple people all over the world add contact names to the same database?
thanks~!
Great I’m glad you fixed it and thank you for posting such a detailed comment to help others.
To set up a shared database it is better to access a database on an outside web server. I plan on covering how to do that soon.
Great, and no problem on the post…
I can connect to DB with web development, just do not know the steps for app development..
Going to continue taking your tutorials and look forward to that topic someday! π
thanks again ~
Hi Derek,
Thanks for the cool tutorials.
I had one problem with this address book app.
When I create new contact, it will be saved.
But in the list view it only shows the contact id.
What is the problem?
Appreciate ur help
You’re very welcome π Try downloading the completed package and testing it. Here it is Android Address Book App. Tell me if that doesn’t work.
Derek
Hey Sir Derek! I like your tutorials about Android. I like them so much because you explain very well and it helped me a lot on my projects. Thank you so much. I hope you could do a tutorial about searching sqlite database. Thank you!!
Thank you very much π I covered SQLite pretty well over the course of a few videos. Have you seen these SQLite Tutorials?
Hello Derek.
First of all I just wanted to tell that you are amazing, (and I know you herd it a lot).
Now for my issue, I have a project to make and its very similar to what you done, the difference is that I need to set the value of my contact from a spinner and a radiobutton.
to get the values from the radio button and the spinner I used queryValuesMap.put(“productQuality”, rgQuality.getContext().toString());
I just dont know how to set it back to their original state.
I hope you understood me correctly, if not I would be more than happy to send you my code so you can review it.
Thank you π Why not take the value and record it some place and then set the spinner / radio button back to the default? I may not understand the problem. I hope that helps in some way.
thank you for trying anyway π
I’m kinda new in the development writing so its really hard for me to explain what I need.
My teacher helped me with that issue, the thing is that i had a spinner that depend on another spinner so it was hard for me to turn it back to its original state, and I did saved the vales in an SQLite database.
I wanted to ask you if you can make a guide for sliding menu using a navigation drawer.
thank you in advance
I will definitely cover that when i get back into regular Java Android tutorials.
Hello Derek. I loved learning how to perform a SQLite project with Java. Unfortunatly, this thing won’t excately run and I don’t know why. When I attempt to, it takes me to the first screen. The second I hit the “ADD” button, it crashes, and its really hurting my head why. I compared my code to yours line by line and I can’t think of why this still fails.
Its like a cruel April’s Fools joke. =(
The debugger was giving me errors along the lines of me not defining my source properly. After messing with that, enough, I thought I fixed it, but still it refuses to work.
Allow me to past what the debugger presently has to say.
04-01 19:36:57.919: W/dalvikvm(2770): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-01 19:36:58.006: E/AndroidRuntime(2770): FATAL EXCEPTION: main
04-01 19:36:58.006: E/AndroidRuntime(2770): java.lang.IllegalStateException: Could not execute method of the activity
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.view.View$1.onClick(View.java:3599)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.view.View.performClick(View.java:4204)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.view.View$PerformClick.run(View.java:17355)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.os.Handler.handleCallback(Handler.java:725)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.os.Looper.loop(Looper.java:137)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-01 19:36:58.006: E/AndroidRuntime(2770): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:36:58.006: E/AndroidRuntime(2770): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 19:36:58.006: E/AndroidRuntime(2770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 19:36:58.006: E/AndroidRuntime(2770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-01 19:36:58.006: E/AndroidRuntime(2770): at dalvik.system.NativeStart.main(Native Method)
04-01 19:36:58.006: E/AndroidRuntime(2770): Caused by: java.lang.reflect.InvocationTargetException
04-01 19:36:58.006: E/AndroidRuntime(2770): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:36:58.006: E/AndroidRuntime(2770): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.view.View$1.onClick(View.java:3594)
04-01 19:36:58.006: E/AndroidRuntime(2770): … 11 more
04-01 19:36:58.006: E/AndroidRuntime(2770): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.codecrunchcorner.addressbookapp/com.codecrunchcorner.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml?
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Activity.startActivityForResult(Activity.java:3370)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Activity.startActivityForResult(Activity.java:3331)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Activity.startActivity(Activity.java:3566)
04-01 19:36:58.006: E/AndroidRuntime(2770): at android.app.Activity.startActivity(Activity.java:3534)
04-01 19:36:58.006: E/AndroidRuntime(2770): at com.codecrunchcorner.addressbookapp.MainActivity.showAddContact(MainActivity.java:79)
04-01 19:36:58.006: E/AndroidRuntime(2770): … 14 more
Can you help me out here? =/
This is the main error Unable to find explicit activity class {com.codecrunchcorner.addressbookapp/com.codecrunchcorner.addressbookapp.NewContact}; have you declared this activity in your AndroidManifest.xml?
Hi Derek!
First of all, I’d like to thank you for providing these awesome tutorials!
I have a problem. When I’m trying to run the app on my phone/emulator, it says “Unfortunately (app name) has stopped working”. What should I do?
Here’s the LogCat.
04-18 06:40:05.854: D/AndroidRuntime(932): Shutting down VM
04-18 06:40:05.854: W/dalvikvm(932): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
04-18 06:40:05.944: E/AndroidRuntime(932): FATAL EXCEPTION: main
04-18 06:40:05.944: E/AndroidRuntime(932): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vitteachersdatabase/com.example.vitteachersdatabase.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.os.Looper.loop(Looper.java:137)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-18 06:40:05.944: E/AndroidRuntime(932): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 06:40:05.944: E/AndroidRuntime(932): at java.lang.reflect.Method.invoke(Method.java:525)
04-18 06:40:05.944: E/AndroidRuntime(932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-18 06:40:05.944: E/AndroidRuntime(932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 06:40:05.944: E/AndroidRuntime(932): at dalvik.system.NativeStart.main(Native Method)
04-18 06:40:05.944: E/AndroidRuntime(932): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-18 06:40:05.944: E/AndroidRuntime(932): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-18 06:40:05.944: E/AndroidRuntime(932): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.Activity.setContentView(Activity.java:1895)
04-18 06:40:05.944: E/AndroidRuntime(932): at com.example.vitteachersdatabase.MainActivity.onCreate(MainActivity.java:25)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.Activity.performCreate(Activity.java:5133)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-18 06:40:05.944: E/AndroidRuntime(932): … 11 more
04-18 06:40:05.944: E/AndroidRuntime(932): Caused by: java.lang.reflect.InvocationTargetException
04-18 06:40:05.944: E/AndroidRuntime(932): at java.lang.reflect.Constructor.constructNative(Native Method)
04-18 06:40:05.944: E/AndroidRuntime(932): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-18 06:40:05.944: E/AndroidRuntime(932): … 25 more
04-18 06:40:05.944: E/AndroidRuntime(932): Caused by: android.content.res.Resources$NotFoundException: File #FFF from drawable resource ID #0x7f05000e: .xml extension required
04-18 06:40:05.944: E/AndroidRuntime(932): at android.content.res.Resources.loadColorStateList(Resources.java:2255)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.widget.TextView.(TextView.java:949)
04-18 06:40:05.944: E/AndroidRuntime(932): at android.widget.TextView.(TextView.java:607)
04-18 06:40:05.944: E/AndroidRuntime(932): … 28 more
04-18 06:40:11.974: I/Process(932): Sending signal. PID: 932 SIG: 9
You’re very welcome π Have you tried downloading the package I have on this page. If it runs then that means you have a typo some place. Use a website like diffnow to compare my files to yours. Just check the manifest, layout folder and src folder. i hope that helps
Hi, your tutorials are absolutely amazing. The application runs but the list does not populate when i add a contact even if i leave all the edittexts empty it still goes back to main activity. what am i doing wrong?
Thank you π I have the whole package available for download on this page. Have you tried that. If my code works then there must be a typo. Then you could compare the files in the layout folder and src folder using something like diffnow. I hope that helps.
Hi Derek,
Thanks a lot for these awesome tutorials. It really helped a newbie like me.
I have 2 scenarios.
I coded the addressbook app by following your videos.
But when i am trying to run it ,it is quitting unexpectedly.
The reason may be ‘fragment_main.xml’. Whenever I am trying to create a new package, It is by default creating the ‘fragment_main.xml’. I am not able to create a package without this ‘fragment_main.xml’.
secondly, I downloaded the package from your website. But still not able to run.I followed your instruction in tutorial -14 to import. After import, It is showing lots of errors in ‘NewContact.java’ file only.Also in the console it is showing ‘Unable to resolve target ‘android 17”. Please let me know if i am doing something wrong.
I show how to fix the Android fragment error here. I’ll be making a new Java Android tutorial very soon.
Hi Derek, how can I add a simple Contact search in this app Thxs in advance.
You’ll have to issue a query to the database. I have an SQLite tutorial that will show how.
That workout pretty good thxs.
“String selectQuery = “SELECT * FROM contacts WHERE lastname LIKE ‘”+ text + “%'”;”
this query did the job. Now It is possible to filter de contactlist without using this method?
Hi Derik!
Tnx for the great tutorials it was really helpful.
I would like to know if it is possible to create two database in the same android application, or create two tables (same database) in the same android app.
Can u please provide me with some links to tutorials covering that subject
Tnx a lot !
Yes you can have multiple databases in one app and multiple tables. Then you can use adb to view the databases. I have an SQLite tutorial here.
Hi Derek!
I’m new to android programming and I was wondering why when I imported your whole project to my eclipse the R.java is missing in the generated package?
Great tutorials btw. thanks.
Jed
Hi Jed, Click Project -> Clean and it will be generated
Hi MASTER DEREK.
I’m fiddling with my first app and another question arose:
suppose I’d like to have several phones/email per contact-
I guess I create another contact-phone TABLE (with contactId is the foreign Key)
how should I deal with those added table regarding insertion-
updating-deleting data.
Thank you so much man!
Hi,
I’d go this route for designing that in the database, but you could also just list each possible phone number in one table
Contact (
ContactId
)
ContactPhone (
ContactId references Contact(ContactId)
PhoneType
PhoneNumber
)
First, thanks for this series.
Second I am having an issue with the listView in activity_main.
Everything works but nothing is displayed. I can tap on the row and it goes to the edit screen (after addeng records). I am not sure if the listAddaptor in not filling in the list (my guess) or the list is white for both text and background.
I suspect all of this is a result of some bug/quirk with Androud Studio.
Thoughts?
Thanks
The code is the same on Android Studio. Have you tried importing the package I provide for this tutorial?