나는 안드로이드 프로그래밍에 초보자이므로 나와 함께 참아주십시오. 이 문제를 해결하기 위해 StackOverFlow를 많이 둘러 보았지만 어떻게 작동하는지 이해할 수 없었습니다. 그래서 나는 나 자신을 만들려고 노력했다. 내가 원하는 것은 전화의 연락처를 listview의 확인란으로 볼 수 있습니다. 이것은 버튼을 클릭했을 때만 발생합니다. 코드는CustomListView with Checkbox
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>
이 코드입니다에 개별 행입니다 아래에 붙여 컴파일 오류없이 실행되지만 ull 포인터 예외.
06-22 10 : 57 : 48.177 : E는/AndroidRuntime (18,986) FATAL EXCEPTION : 메인 06-22 10 : 57 : 48.177 : E/AndroidRuntime (18,986) 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) : 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) : at> android.app.ActivityThread.main (ActivityThread.java:5041) 06-22 10 : 57 : 48.177 : E/AndroidRuntime (18986) : at> java.lang.reflect.Method.invokeNative 네이티브 메소드) 06-22 10 : 57 : 48.177 : E/AndroidRuntime (18986) : java.lang.reflect.Method.invoke (Method.java:511) 06-22 10 : 57 : 48.177 : E/AndroidRuntime (18986) :> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793)에서
사람이 내가 잘못 여기서 뭐하는 거지 말씀해 주시겠습니까? 고마워요 고마워요.
하십시오, 그것은 어떤 문자열 쓰기를 당신의
Listview
ID를 변경하고 작동하는지 확인하려고? AddContacts.java:68 – Dimmerg@Dimmerg - 충돌합니다. list.setAdapter (adapter); – user2511882
다음 중 귀하의 목록이나 어댑터가 null입니다 .. 디버그 모드에서 확인해보십시오 –