Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomListView with Checkboxes
    primarykey
    data
    text
    <p>I am a novice with at android programming so please bear with me. I have looked around a lot on StackOverFlow for solution for this but I couldn't understand how it works. So I tried to make one myself. All I want is to be able to view the contacts in a phone in a <em>listView</em> with <em>checkboxes</em>. This should only happen when a button is clicked. The code is pasted below</p> <pre><code>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.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class AddContacts extends Activity implements OnClickListener { static final String[] FROM = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; static final int[] TO = { R.id.contact_name, R.id.phone_number }; ListView list; Cursor cursor; SimpleCursorAdapter adapter; Button AddNumber; EditText number1; EditText number2; EditText number3; String contact1; String contact2; static final String TAG = "In AddContacts"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondscreen); AddNumber = (Button) findViewById(R.id.AddNumbers); number1 = (EditText) findViewById(R.id.NumberField1); number2 = (EditText) findViewById(R.id.NumberField2); number3 = (EditText) findViewById(R.id.NumberField3); AddNumber.setOnClickListener(this); list = (ListView)findViewById(R.id.list); Log.d(TAG, "onCreate"); } @Override public void onClick(View v) { Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; cursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder); adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO); list.setAdapter(adapter); // Intent intents = new Intent(Intent.ACTION_PICK, // ContactsContract.Contacts.CONTENT_URI); // startActivityForResult(intents, PICK_CONTACT); contact1 = number1.getText().toString(); if (!contact1.matches(regularExpression)) { AddNumber.setEnabled(false); } Log.d(TAG, "onClicked method"); Intent intent = new Intent(AddContacts.this, TrackLogic.class); intent.putExtra("contact1", contact1); Log.d(TAG, "contact values are: " + contact1); startActivity(intent); } } </code></pre> <p>This code is for the individual row in the ListView </p> <pre><code>&lt;TextView android:id="@+id/contact_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="John Doe" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/phone_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/contact_name" android:text="(999)999-9999" android:textAppearance="?android:attr/textAppearanceSmall" /&gt; &lt;CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="CheckBox" /&gt; </code></pre> <p></p> <p>This code is for the ListView</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView style="@style/AppTheme" /&gt; &lt;ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>This code run without an compilation error but crashes with a null pointer exception.</p> <blockquote> <p>06-22 10:57:48.177: E/AndroidRuntime(18986): FATAL EXCEPTION: main 06-22 10:57:48.177: E/AndroidRuntime(18986): java.lang.NullPointerException 06-22 10:57:48.177: E/AndroidRuntime(18986): at >com.example.s.AddContacts.onClick(AddContacts.java:68) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.view.View.performClick(View.java:4204) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.view.View$PerformClick.run(View.java:17355) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.os.Handler.handleCallback(Handler.java:725) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.os.Handler.dispatchMessage(Handler.java:92) 06-22 10:57:48.177: E/AndroidRuntime(18986): at android.os.Looper.loop(Looper.java:137) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.app.ActivityThread.main(ActivityThread.java:5041) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >java.lang.reflect.Method.invokeNative(Native Method) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >java.lang.reflect.Method.invoke(Method.java:511) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)</p> </blockquote> <p>Can anyone tell me what am i doing wrong here? It would be great Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload