-1

学位取得用の Android アプリケーションを作成しています。アプリケーションを開始するために電話からすべての連絡先を取得する必要があります(後でメールなども取得する必要があります)。だから私は2つのクラスを使いました

Degree_thesisActivity

   public class Degree_thesisActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GetFromPhone gf = new GetFromPhone();
    gf.getContacts();

}

}

GetFromPhone.java

class GetFromPhone extends Activity {

public void getContacts() {
    // TODO Auto-generated method stub


    /**Declare a Contacts Cursor which have a list of all contacts*/
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    /**Move while cursor has another istance to process*/
    while(cursor.moveToNext()){
        /**Get the contact name pointed by cursor at that time*/
        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        /**show the contact pointed.THIS WILL BE MODIFIED FURTHER!!!!*/
                new AlertDialog.Builder(this)
                    .setMessage(contactID)
                    .setNeutralButton("ok", new OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    })
                    .show();

    }

}

}

しかし、私はこのエラーが発生します:

05-05 15:49:20.060: E/AndroidRuntime(824): java.lang.RuntimeException: アクティビティ ComponentInfo を開始できません {it.paki.degreethesis/it.paki.degreethesis.Degree_thesisActivity}: java.lang.NullPointerException

なぜ?誰かが私を助けることができますか?

4

1 に答える 1

1

クラスGetFromPhoneは extendsActivityであるため、キーワードを使用してクラスのインスタンスを作成することはできません。newとして、アクティビティのライフサイクルActivityに従う必要があります。アプリケーションの基礎を読む必要があると思います。

于 2012-05-05T20:37:24.557 に答える