我是android编程的新手,所以请多多包涵。我在 StackOverFlow 上寻找了很多解决方案,但我不明白它是如何工作的。所以我试着自己做一个。我想要的只是能够在带有复选框的listView中查看电话中的联系人。这应该只在单击按钮时发生。代码粘贴在下面
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);
}
}
此代码适用于 ListView 中的单个行
<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" />
<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" />
<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" />
此代码适用于 ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView style="@style/AppTheme" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
此代码在没有编译错误的情况下运行,但由于空指针异常而崩溃。
06-22 10:57:48.177:E/AndroidRuntime(18986):致命异常:主要 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): 在 android.os.Looper.loop(Looper.java:137) 06-22 10:57:48.177: E/AndroidRuntime(18986) ): 在 >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)
谁能告诉我我在这里做错了什么?太好了,谢谢。