无法启动活动ComponentInfo:java.lang.NullPointerException

时间:2012-05-05 16:15:06

标签: java android

我为我的学位建立一个Android应用程序。我需要通过电话联系所有联系人 开始应用(后来我还需要收到电子邮件etcetc)。所以我用了两个班级

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

为什么?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

由于课程GetFromPhone延伸Activity,您无法使用new关键字来创建课程的实例。作为Activity,它需要遵循activity lifecycle。我想你需要阅读application fundamentals