我在跟随初学者Android开发程序时遇到了一些麻烦。
我正在建立一种联系人管理员,我正试图立即实现一种呼叫联系人的方式。
然而,自从添加callButton后,我在第一个Button下面的行上出现了这个错误,这个:
button.setOnClickListener(new public void OnClick(View v){Listener(){"
我真的不明白错误,所以任何帮助都会非常感谢。
public class ContactPickerTester extends Activity {
public static final int PICK_CONTACT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_picker_tester);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new public void OnClick(View v){Listener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri
.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
Button insertContactButton = (Button) findViewById(R.id.button2);
insertContactButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
insertContactWithIntent();
}
});
}
private void insertContactWithIntent() {
//inserting a new contact using intents//
Intent intent = new Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
startActivity(intent);
Button callButton = (Button) findViewById(R.id.button3);
callButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
Intent myIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("content://contacts/"));
startActivity(myIntent);
}
});
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data);
switch (reqCode) {
case (PICK_CONTACT): {
if (resCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
String name = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
tv.setText(name);
}
break;
}
}
}
}
答案 0 :(得分:1)
创建匿名内部类的语法有点混乱。该行应该是:
button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri
.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
}
});