Android: Crashed when single contact is clicked

Posted by Sean Tan on Stack Overflow See other posts from Stack Overflow or by Sean Tan
Published on 2012-12-05T04:22:12Z Indexed on 2012/12/05 5:04 UTC
Read the original article Hit count: 421

Filed under:
|
|

My application is always crashed at this moment, guru here please help me to solved. Thanks.The situation now is as mentioned in title above. Hereby is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.contactmanager"
    android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.WRITE_OWNER_DATA"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

    <application android:label="@string/app_name" android:icon="@drawable/icon" android:allowBackup="true">
       <!--  --><activity android:name=".ContactManager" android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="ContactAdder" android:label="@string/addContactTitle">
        </activity>

        <activity android:name=".SingleListContact"
                    android:label="Contact Person Details">
        </activity>

    </application>

</manifest>

The SingleListContact.java

package com.example.android.contactmanager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleListContact extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_contact_view);

        TextView txtContact = (TextView) findViewById(R.id.contactList);

        Intent i = getIntent();
        // getting attached intent data
        String contact = i.getStringExtra("contact");
        // displaying selected product name
        txtContact.setText(contact);

    }
}

My ContactManager.java as below

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.contactmanager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public final class ContactManager extends Activity implements OnItemClickListener
{

    public static final String TAG = "ContactManager";

    private Button mAddAccountButton;
    private ListView mContactList;
    private boolean mShowInvisible;
    //public BooleanObservable ShowInvisible = new BooleanObservable(false);
    private CheckBox mShowInvisibleControl;

    /**
     * Called when the activity is first created. Responsible for initializing the UI.
     */



    @Override
    public void onCreate(Bundle savedInstanceState)
    {


        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_manager);

        // Obtain handles to UI objects
        mAddAccountButton = (Button) findViewById(R.id.addContactButton);
        mContactList = (ListView) findViewById(R.id.contactList);
        mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);

        // Initialise class properties
        mShowInvisible = false;
        mShowInvisibleControl.setChecked(mShowInvisible);

        // Register handler for UI elements
        mAddAccountButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d(TAG, "mAddAccountButton clicked");
                launchContactAdder();
            }
        });
        mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
                mShowInvisible = isChecked;
                populateContactList();
            }
        });

        mContactList = (ListView) findViewById(R.id.contactList);
        mContactList.setOnItemClickListener(this);

        // Populate the contact list
        populateContactList();
    }



    /**
     * Populate the contact list based on account currently selected in the account spinner.
     */
    private void populateContactList() {
        // Build adapter with contact entries
        Cursor cursor = getContacts();
        String[] fields = new String[] {
                ContactsContract.Data.DISPLAY_NAME
        };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
                fields, new int[] {R.id.contactEntryText});
        mContactList.setAdapter(adapter);
    }

    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[]
                {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
                };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'";
        //String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible.get() ? "0" : "1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return this.managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

    /**
     * Launches the ContactAdder activity to add a new contact to the selected account.
     */
    protected void launchContactAdder()
    {
        Intent i = new Intent(this, ContactAdder.class);
        startActivity(i);
    }

    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
        Log.i("TAG", "You clicked item " + id + " at position " + position);
        // Here you start the intent to show the contact details
     // selected item
        TextView tv=(TextView)v.findViewById(R.id.contactList);
        String allcontactlist = tv.getText().toString();

        // Launching new Activity on selecting single List Item
        Intent i = new Intent(getApplicationContext(), SingleListContact.class);
        // sending data to new activity
        i.putExtra("Contact Person", allcontactlist);
        startActivity(i);

    }


}

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">  


    <ListView android:layout_width="wrap_content"
              android:id="@+id/contactList"
              android:layout_height="0dp"
              android:padding="10dp"
              android:textSize="200sp"
              android:layout_weight="10"/>
    <CheckBox android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/showInvisible"
              android:text="@string/showInvisible"/>
    <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/addContactButton"
            android:text="@string/addContactButtonLabel"/>
</LinearLayout>

Logcat result:

12-05 05:00:31.289: E/AndroidRuntime(642): FATAL EXCEPTION: main
12-05 05:00:31.289: E/AndroidRuntime(642): java.lang.NullPointerException
12-05 05:00:31.289: E/AndroidRuntime(642):  at com.example.android.contactmanager.ContactManager.onItemClick(ContactManager.java:148)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.widget.ListView.performItemClick(ListView.java:3513)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.os.Handler.handleCallback(Handler.java:587)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.os.Looper.loop(Looper.java:123)
12-05 05:00:31.289: E/AndroidRuntime(642):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-05 05:00:31.289: E/AndroidRuntime(642):  at java.lang.reflect.Method.invokeNative(Native Method)
12-05 05:00:31.289: E/AndroidRuntime(642):  at java.lang.reflect.Method.invoke(Method.java:507)
12-05 05:00:31.289: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-05 05:00:31.289: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-05 05:00:31.289: E/AndroidRuntime(642):  at dalvik.system.NativeStart.main(Native Method)

© Stack Overflow or respective owner

Related posts about android

Related posts about crash