0

私は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>

このコードはコンパイル エラーなしで実行されますが、null ポインター例外でクラッシュします。

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): >com.example.s.AddContacts.onClick(AddContacts.java:68) 06-22 10:57:48.177: E/AndroidRuntime(18986): >android.view.View.performClick (View.java:4204) 06-22 10:57:48.177: E/AndroidRuntime(18986): >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): >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): >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)

ここで私が間違っていることを誰かに教えてもらえますか? ありがとうございます。

4

2 に答える 2

0

setContentView** ()に他の xml ファイルをロードしているため、Listview初期化されないため、NullPointerException

の代わりに
アクセスしていると思われる他の理由が考えられます。android.R.id.listyour-package-name.R.id.list

IDを変更して、Listview動作するかどうかを確認してください。

お役に立てれば....

于 2013-06-22T17:32:21.787 に答える