Android随机来电者

时间:2017-01-12 13:25:50

标签: android

我正在试图制作一个程序,可以从我的联系人那里打电话给我,我试着先把所有联系人都放在一个arraylist中,并使用randomgenerator方法调用联系人作为我的arraylist中的索引。但是这个代码有问题。在arraylist中接触联系人并不完整,但是当我按下按钮应用程序正在关闭时。为什么?

package com.example.uskudar.randomcaller;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.*;
import android.database.Cursor;
import java.util.ArrayList;
import android.provider.ContactsContract;

import android.util.Log;

public class MainActivity extends AppCompatActivity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
Context context;

ArrayList<String> contacts = new ArrayList<>();

public void addContacts(){

    //to store name-number pair
    String obj = new String();

    try {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while (phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            obj = phoneNumber;
            contacts.add(obj);

            Log.e("Contact list ", " "+contacts);
        }
        phones.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

public  void callRandom(View view){

    addContacts();


}
}

这是logcat

01-12 15:34:52.846 3695-3695 / com.example.uskudar.randomcaller E / AndroidRuntime:FATAL EXCEPTION:main                                                                                 过程:com.example.uskudar.randomcaller,PID:3695                                                                                 java.lang.IllegalStateException:找不到父或祖先的方法callRandom(MainActivity)(View)上下文中的android:onClick属性在视图类android.support.v7.widget.AppCompatButton上定义,ID为&#39; button&#39 ;                                                                                     在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)                                                                                     在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                                                                     在android.view.View.performClick(View.java:5610)                                                                                     在android.view.View $ PerformClick.run(View.java:22260)                                                                                     在android.os.Handler.handleCallback(Handler.java:751)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                     在android.os.Looper.loop(Looper.java:154)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

您在评论中发布的xml说android:onClick="callRandom (MainActivity)",这会导致默认点击侦听器在MainActivity或其父类中查找方法public void callRandom (MainActivity)(View v)。您可能会注意到这不是正确的Java语法,而这又是导致问题的原因。