“新的View.OnClickListener(){}类型必须实现继承的抽象方法View.OnClickListener.OnClick(View)

时间:2014-04-28 20:46:14

标签: android

我正在浏览android dev的初学者教程,我不明白这个错误。

错误出现在“callButton.setOnClickListener”(新的OnClickListener(){“

是的,我知道代码写得很糟糕,有点令人困惑,我会在工作后清理它。

我正在尝试添加一个按钮,打开联系人列表并允许您拨打电话。 package com.example.contactpicker;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.Intents;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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 OnClickListener() {


            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;
        }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

这很简单,因为你在“O”上用大写写了OnClick,它是一个小写。

所以你必须替换

public void OnClick(View v){

通过

public void onClick(View v){

请注意大写更改。