1

The parseInt error thing has resolved last time so thanks a lot. Now, this time, the problem is with running the app. Though I find no errors in the code, I can't run the program and it force close saying "Unfortunately, has stopped"

Is there something I did wrong or is there something missing from my system? I'm using (again) the 1.1.0 version of AndroidStudio. I'll be providing my MainActivity, Contact class and DBHandler class to help you guys out and please do try to run it. I hope you could help me resolve this issue. Thank you.

MAIN ACTIVITY:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtName = (EditText) findViewById(R.id.txtName);
    txtPhone = (EditText) findViewById(R.id.txtPhone);
    txtEmail = (EditText) findViewById(R.id.txtEmail);
    txtAddress = (EditText) findViewById(R.id.txtAddress);
    ContactListView = (ListView)findViewById(R.id.listView);
    contactImageimgView = (ImageView) findViewById(R.id.imgViewContactPhoto);
    TabHost th = (TabHost) findViewById(R.id.tabHost);
    dbhand = new DBHandler(getApplicationContext());

    th.setup();

    TabHost.TabSpec ts = th.newTabSpec("Creator");
    ts.setContent(R.id.Creator);
    ts.setIndicator("Creator");
    th.addTab(ts);

    ts = th.newTabSpec("List");
    ts.setContent(R.id.SaveContacts);
    ts.setIndicator("List");
    th.addTab(ts);


    final Button addBtn = (Button) findViewById(R.id.addBtn);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Contact contact = new Contact(dbhand.getContactsCount(), String.valueOf(txtName.getText()), String.valueOf(txtPhone.getText()), String.valueOf(txtEmail.getText()), String.valueOf(txtAddress.getText()), imageURI);
            dbhand.createContact(contact);
            Contacts.add(contact);
            populateList();
            Toast.makeText(getApplicationContext(), txtName.getText().toString() + " has been saved",
                    Toast.LENGTH_SHORT).show();
        }
    });

    txtName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            addBtn.setEnabled(!txtName.getText().toString().trim().isEmpty());
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    contactImageimgView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select a contact image"), 1);
        }
    });

    List<Contact> addableContacts = dbhand.getAllContacts();
    int contactCount = dbhand.getContactsCount();

    for(int i = 0; i < contactCount; i++) {
        Contacts.add(addableContacts.get(i));
    }

    if(!addableContacts.isEmpty()) {
        populateList();
    }

}

public void onActivityResult(int reqCode, int resCode, Intent data) {
    if(resCode == RESULT_OK) {
        if(reqCode == 1) {
            imageURI = (Uri) data.getData();
            contactImageimgView.setImageURI(data.getData());
        }
    }
}

private void populateList() {
    ArrayAdapter<Contact> adapter = new ContactListAdapter();
    ContactListView.setAdapter(adapter);
}


private class ContactListAdapter extends ArrayAdapter {
    public ContactListAdapter() {
        super (MainActivity.this, R.layout.listview_item, Contacts);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if(view == null)
            view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);

            Contact currentContact = Contacts.get(position);

            TextView name = (TextView) view.findViewById(R.id.cName);
            name.setText(currentContact.getName());
            TextView phone = (TextView) view.findViewById(R.id.cPhone);
            phone.setText(currentContact.getPhone());
            TextView email = (TextView) view.findViewById(R.id.cEmail);
            email.setText(currentContact.getEmail());
            TextView address = (TextView) view.findViewById(R.id.cAddress);
            address.setText(currentContact.getAddress());
            ImageView ivContactImg = (ImageView) view.findViewById(R.id.iView);
            ivContactImg.setImageURI(currentContact.getimageURI());

            return view;

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

CONTACT CLASS: package com.example.contactsbeta;

private String _name, _phone, _email, _address;
private Uri _imageURI;
private int _id;

public Contact (int id, String name, String phone, String email, String address, Uri imageURI) {

    _id = id;
    _name = name;
    _phone = phone;
    _email = email;
    _address = address;
    _imageURI = imageURI;

}



public int getId() {return _id;}
public String getName() {
    return _name;
}
public String getPhone() {
    return _phone;
}
public String getEmail() {
    return _email;
}
public String getAddress() {
    return _address;
}
public Uri getimageURI() {return _imageURI;}

DATABASE HANDLER:

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "contactManager",
TABLE_CONTACTS = "contacts",
KEY_ID = "id",
KEY_NAME = "name",
KEY_PHONE = "phone",
KEY_EMAIL = "email",
KEY_ADDRESS = "address",
KEY_IMAGEURI = "imageUri";

    public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " TEXT, " + KEY_PHONE + " TEXT, " + KEY_EMAIL + " TEXT, " +
            KEY_ADDRESS + " TEXT, " + KEY_IMAGEURI + " TEXT) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    onCreate(db);
}

public void createContact(Contact contact) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PHONE, contact.getPhone());
    values.put(KEY_EMAIL, contact.getEmail());
    values.put(KEY_ADDRESS, contact.getAddress());
    values.put(KEY_IMAGEURI, contact.getimageURI().toString());

    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}

public Contact getContact(int id) {
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS, KEY_IMAGEURI },
            KEY_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null );

    if(cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));


    db.close();
    cursor.close();

    return contact;

}

public void DeleteContact(Contact contact) {
    SQLiteDatabase db = getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " =?", new String[] {String.valueOf(contact.getId())});
    db.close();
}

public int getContactsCount() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
    int count = cursor.getCount();

    db.close();
    cursor.close();

    return count;

}

public int updateContact(Contact contact) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PHONE, contact.getPhone());
    values.put(KEY_EMAIL, contact.getEmail());
    values.put(KEY_ADDRESS, contact.getAddress());
    values.put(KEY_IMAGEURI, contact.getimageURI().toString());

    return db.update(TABLE_CONTACTS, values, KEY_ID + " =?", new String[] {String.valueOf(contact.getId())});

}

public List<Contact> getAllContacts() {
    List<Contact> contacts = new ArrayList<Contact>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
    if(cursor.moveToFirst()) {
        do {
            Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
                    cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
            contacts.add(contact);
        } while (cursor.moveToNext());
    }
    return contacts;
}
  • Your call to `Integer.parseInt` is incorrect - you need to call it once for each item you're trying to parse, i.e. `Integer.parseInt(cursor.getString(0), cursor.getString(1), cursor.getString(2),...` should actually be `Integer.parseInt(cursor.getString(0)), Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)),...` – Simon MᶜKenzie Apr 16 '15 at 02:01
  • Good day, sir! Your solution worked and the error has been neutralized. Thank you so much, sir. I just hope the application will work. *crosses fingers* – Benedict Gesmundo Apr 17 '15 at 02:50
  • Start with the stacktrace. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Apr 17 '15 at 07:40
  • Good day, laalto, I start doing it but it confuses me a lot more and I can't even analyze what was going on there. Again, I'm a first time developer of Android Studio and I can't seem to understand what went wrong with the program even though the code has no errors – Benedict Gesmundo Apr 19 '15 at 07:32

1 Answers1

0

Try this,

Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));

contacts.add(contact);
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45