
Originally Posted by
raj_J2ME
What are the MIDP and CLDC version of the app?
Please compare the same with the device's ?
Which device are you working with?
have checked the MIDP and CLDC version...when i removed the PIM from the Constructor that error goes off..but the problem is
the contact is not being added up.. am i making mistakes in the case of header files.
Am pasting my code below..
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;
import java.util.*;
public class MyPim extends MIDlet implements CommandListener {
private Display display;
private Form myForm;
private Form newContactForm;
/****************** Text Fields *************************/
private TextField enterName;
private TextField enterNick;
private TextField enterNumber;
/****************** ContactList **************************/
ContactList myContactList = null;
Contact c = null;
/***************** Enumeration object for storing contacts in **********/
Enumeration contact;
/***************** Commands ******************************/
//private Command contactDB;
private Command displayPhonebook;
private Command enterContact; //Take to contact input form
private Command addContact; //Commit contact to DB
private Command showFields;
private Command showFieldAttributes;
private Command showCategories;
private Command clearScreen;
//PIMS
PIM myPIM;
public MyPim(){
display = Display.getDisplay(this);
myForm = new Form("Contact PIM");
displayPhonebook = new Command("PhoneBook",Command.OK,0);
enterContact = new Command("EnterContact",Command.OK,0);
showFields = new Command("Fields",Command.OK,0);
showFieldAttributes = new Command("Attributes",Command.OK,0);
showCategories = new Command("Categories",Command.OK,0);
clearScreen = new Command("Clear Screen",Command.OK,0);
myForm.addCommand(displayPhonebook);
myForm.addCommand(enterContact);
myForm.addCommand(showFields);
myForm.addCommand(showFieldAttributes);
myForm.addCommand(showCategories);
myForm.addCommand(clearScreen);
myForm.setCommandListener(this);
display.setCurrent(myForm);
//myPIM = PIM.getInstance();
//initPIM();
}
private void initPIM(){
myForm.deleteAll();
myPIM = PIM.getInstance();
try {
String ContactList[] = myPIM.listPIMLists(PIM.CONTACT_LIST);
myContactList = (ContactList)myPIM.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
contact = myContactList.items();
}
catch (Exception e) {
e.printStackTrace();
}
DisplayContacts();
}
/*How to Create a New Contact*/
public void DisplayNewContactForm(){
newContactForm = new Form("Enter new contact");
addContact = new Command("Add", Command.OK, 0);
enterName = new TextField("Enter Name","", 20,TextField.ANY);
enterNick = new TextField("Enter Nick","", 20,TextField.ANY);
enterNumber = new TextField("Enter Tel","", 20,TextField.PHONENUMBER);
newContactForm.append(enterName);
newContactForm.append(enterNick);
newContactForm.append(enterNumber);
newContactForm.addCommand(addContact);
newContactForm.addCommand(displayPhonebook);
newContactForm.setCommandListener(this);
display.setCurrent(newContactForm);
}
public void createNewContact(){
String getName = enterName.getString();
String getNick = enterNick.getString();
String getNumber = enterNumber.getString();
Contact newContact = myContactList.createContact();
//Name
if (myContactList.isSupportedField(Contact.FORMATTED_NAME)){
try{
newContact.addString(Contact.FORMATTED_NAME,PIMItem.ATTR_NONE,getName);
}catch(Exception e){
e.printStackTrace();
}
}
else{
System.out.println("THIS IS NAME");
}
//NickName
if (myContactList.isSupportedField(Contact.NICKNAME)){
try{
newContact.addString(Contact.NICKNAME,Contact.ATTR_MOBILE, getNick);
}catch(Exception e){
e.printStackTrace();
}
}
//Telephone
if (myContactList.isSupportedField(Contact.TEL)){
try{
newContact.addString(Contact.TEL, PIMItem.ATTR_NONE,getNumber);
}catch(Exception e){
e.printStackTrace();
}
}
try{
//write the new Contact to the PIM database
newContact.commit();
System.out.println("Contact Added \n");
}
catch(Exception e){
e.printStackTrace();
}
}
/* below is used for displaying all the "fields" present */
public void displayContactListFields(){
int[] arrayFields = myContactList.getSupportedFields();
myForm.append("Supported Fields \n");
for(int i = 0; i < arrayFields.length ; i++){
try{
myForm.append(myContactList.getFieldLabel(arrayFields[i]) + "\n");
}catch(Exception e){
e.printStackTrace();
}
}
}
/* below is used for displaying all the "Categories" present */
//To Find the list of supported fields on your device
public void displayContactListCategories(){
try{
String[] listCategories = myContactList.getCategories();
myForm.append("Supported Categories");
for(int i = 0; i < listCategories.length ; i++){
myForm.append(listCategories[i] + "\n");
}
}
catch(Exception e){
e.printStackTrace();
}
}
/* below is used for displaying all the "Attributes" present */
public void displayFieldAttributes(){
int[] arrayFields = myContactList.getSupportedFields();
for(int i = 0; i < arrayFields.length ; i++){
try{
myForm.append("\n" +myContactList.getFieldLabel(arrayFields[i]) + " : ");
int attrList[] = myContactList.getSupportedAttributes(arrayFields[i]);
for(int j=0;j<attrList.length;j++){
myForm.append(myContactList.getAttributeLabel(attrList[j])+ " ");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public void DisplayContacts(){
while (contact.hasMoreElements()) {
c = (Contact) contact.nextElement();
int[] Fields = c.getFields();
String[] catArray = c.getCategories();
for(int ii = 0; ii < catArray.length ; ii++){
myForm.append("Category :" + catArray[ii] + "\n");
System.out.println("Category :" + catArray[ii]);
}
for(int i = 0; i < Fields.length ; i++){
String field = myContactList.getFieldLabel(Fields[i]);
if(Fields[i] == Contact.FORMATTED_NAME){
myForm.append("Name : " + c.getString(Fields[i],0) + "\n");
System.out.println("Name : " +c.getString(Fields[i],0));
}
if(Fields[i] == Contact.TEL){
int att = c.getAttributes(Fields[i],0);
String attributeLabel = myContactList.getAttributeLabel(att);
System.out.println(attributeLabel + c.getString(Fields[i],0));
if(attributeLabel.compareTo("NONE") < 0){
myForm.append(attributeLabel + " : " +c.getString(Fields[i],0) + "\n");
}
else if(attributeLabel.compareTo("NONE") < 0){
myForm.append("Telephone : "+c.getString(Fields[i],0) + "\n");
}
}
}
}
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(c == displayPhonebook){
initPIM();
display.setCurrent(myForm);
}
else if(c == enterContact){
DisplayNewContactForm();
}
else if(c == addContact){
createNewContact();
DisplayNewContactForm();
}
else if (c == showFields){
displayContactListFields();
}
else if(c == showFieldAttributes){
displayFieldAttributes();
}
else if(c == showCategories){
displayContactListCategories();
}
else if(c == clearScreen){
myForm.deleteAll();
}
}
}
Can u please help me..
With Regars
Deva