[英]The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments ()
I'm having some trouble whilst following a beginner android dev program. 在遵循初学者的android开发程序时遇到了一些麻烦。
I am making a sort of contacts manager, and I am trying to implement right now a way to call contacts. 我正在创建一种联系人管理器,并且我现在正在尝试实现一种呼叫联系人的方法。
However since adding the callButton I have had this error on the line below my first Button, this: 但是,由于添加了callButton,因此我在第一个Button下方的行上遇到了此错误,这是:
button.setOnClickListener(new public void OnClick(View v){Listener() {" button.setOnClickListener(new public void OnClick(View v){Listener(){“
I don't really understand the error so any help would be great thanks. 我不太了解错误,因此非常感谢您的帮助。
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;
}
}
}
}
Your syntax for creating an anonymous inner class is a little mixed up. 创建匿名内部类的语法有些混乱。 That line should be:
该行应为:
button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri
.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.