2015-10-18 68 views
0

我的應用程序出現問題。我想要做的是當我按下按鈕來打開我的電話Contatcs並將它們顯示到我的TextViews。當我執行Button Press時,我有問題。當按下按鈕時點擊聯繫人onClick Android

至少LogCat是說我一些錯誤與我的按鈕,但我不明白什麼是錯的。

在我的Manifestfile中,我已經設置了讀寫聯繫人的權限。我有一個ContactView.java代碼發生,我已經創建了按鈕和2個textView的視圖。

也許有人可以幫助我,或者至少告訴我我做錯了什麼。

在我看來,我說:安卓的onClick = 「pickContact」

import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 

public class ContactView extends AppCompatActivity { 
private static final int RESULT_PICK_CONTACT = 85500; 
private TextView textView1; 
private TextView textView2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contact_view); 
    textView1 = (TextView) findViewById(R.id.TxtName); 
    textView2 = (TextView) findViewById(R.id.TxtNumber); 
} 
public void pickContact(View v) 
{ 
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // check whether the result is ok 
    if (resultCode == RESULT_OK) { 
     // Check for the request code, we might be usign multiple startActivityForReslut 
     switch (requestCode) { 
      case RESULT_PICK_CONTACT: 
       contactPicked(data); 
       break; 
     } 
    } else { 
     Log.e("ContactView", "Failed to pick contact"); 
    } 
} 
/** 
* Query the Uri and read contact details. Handle the picked contact data. 
* @param data 
*/ 
private void contactPicked(Intent data) { 
    Cursor cursor = null; 
    try { 
     String phoneNo = null ; 
     String name = null; 
     // getData() method will have the Content Uri of the selected contact 
     Uri uri = data.getData(); 
     //Query the content uri 
     cursor = getContentResolver().query(uri, null, null, null, null); 
     cursor.moveToFirst(); 
     // column index of the phone number 
     int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     // column index of the contact name 
     int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     phoneNo = cursor.getString(phoneIndex); 
     name = cursor.getString(nameIndex); 
     // Set the value to the textviews 
     textView1.setText(name); 
     textView2.setText(phoneNo); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

這是logcat的所說的話後,我按我的addcontacts在調試按鈕:

10-18 23:11:08.684 4353-4353/me.resq.resqme E/AndroidRuntime﹕ FATAL EXCEPTION: main 
     Process: me.resq.resqme, PID: 4353 
     java.lang.IllegalStateException: Could not execute method of the activity 
       at android.view.View$1.onClick(View.java:3969) 
       at android.view.View.performClick(View.java:4640) 
       at android.view.View$PerformClick.run(View.java:19421) 
       at android.os.Handler.handleCallback(Handler.java:733) 
       at android.os.Handler.dispatchMessage(Handler.java:95) 
       at android.os.Looper.loop(Looper.java:136) 
       at android.app.ActivityThread.main(ActivityThread.java:5476) 
       at java.lang.reflect.Method.invokeNative(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:515) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
       at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.reflect.InvocationTargetException 
       at java.lang.reflect.Method.invokeNative(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:515) 
       at android.view.View$1.onClick(View.java:3964) 
                at android.view.View.performClick(View.java:4640) 
                at android.view.View$PerformClick.run(View.java:19421) 
                at android.os.Handler.handleCallback(Handler.java:733) 
                at android.os.Handler.dispatchMessage(Handler.java:95) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5476) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode 
       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:746) 
       at me.resq.resqme.ContactView.pickContact(ContactView.java:28) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at android.view.View$1.onClick(View.java:3964) 
                at android.view.View.performClick(View.java:4640) 
                at android.view.View$PerformClick.run(View.java:19421) 
                at android.os.Handler.handleCallback(Handler.java:733) 
                at android.os.Handler.dispatchMessage(Handler.java:95) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5476) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                at dalvik.system.NativeStart.main(Native Method) 

回答

0
private static final int RESULT_PICK_CONTACT = 85500; 

請嘗試將其更改爲如下所示的較低值:

private static final int RESULT_PICK_CONTACT = 85; 
+0

實際上工作...這是擺脫這個問題的正確方法? – idunnowhatimdoin

+0

@idunnowhatimdoin我沒有讀過你的整個堆棧跟蹤,我的回答是錯誤的,所以我刪除了它。有關這裏發生了什麼的更多解釋:http://stackoverflow.com/questions/25529865/java-lang-illegalargumentexception-can-only-use-lower-16-bits-for-requestcode –