2014-02-27 122 views
4

喜在片段我要挑从通讯录中的电话号码,并嵌入成的EditText使用onActivityResult在片段

,但它不是在片段工作,我用它的活性和它的作品。请你能帮助我,我应该如何改变它<感谢

public class Encrypt extends Fragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.encrypt, null); 
     phoneNumberDisplay = (EditText) v.findViewById(R.id.editTextPhoneNumber); 
     contactsButton=(Button)v.findViewById(R.id.buttonContacts); 
     contactsButton.setOnClickListener(new OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       if(v==contactsButton){ 
        Intent intent=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); 
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
        startActivityForResult(intent, 1); 
       } 
      } 
     }); 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (resultCode == RESULT_OK) { 
       Uri ur = data.getData(); 
       Cursor c = managedQuery(ur, null, null, null, null); 
       if (c.moveToFirst()) { 
        String s = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        phoneNumberDisplay.setText(s); 
       } 
      } 
     } 
     return v; 
    } 

错误:RESULT_OK不能被解析为一个变量

的方法managedQuery(URI,NULL,NULL,NULL,NULL)是不确定的键入新 View.OnClickListener(){}

+0

看起来好,您是否尝试调试并检查是否已调用onActivityResult? –

+0

@UdiOshi它不可能运行它强调onActivityResult和写:void是一个无效类型的变量onActivityResult – rgreso

+0

@rgreso看到我的答案,它可能会解决您的问题。 – Synxis

回答

0

对于访问联系人形成你的电话,请确保您添加清单文件的权限android.permission.READ_CONTACTS ..

+0

是的,我有这个permision ..它给我写的是:void是变量的无效类型onActivityResult – rgreso

+0

试试这个,,,可以解决你的问题.. – Akshay

+0

@Override protected void onActivityResult(int requestCode,int resultCode,Intent data){if(resultCode = = RESULT_OK) \t \t \t \t return; Uri ur = data.getData();光标c = managedQuery(ur,null,null,null,null); (c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));如果(c.moveToFirst()){c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));如果(c.moveToFirst()){c0.moveToFirst() \t \t \t \t phoneNumberDisplay.setText(s); \t \t \t} \t \t} – Akshay

0

有可能有两种PO这是可能的原因。

  1. 呼叫使用活动的参考你的statrtActivityForResults,像

    getActivity().startActivityForResult(intent, 1); 
    
  2. 调用super.onActivityResult(requestCode,resultCode为,数据),你onActivityResult()作为@marcin_j也指出。

尝试其中之一或两者。看看哪一个适合你。

这里有一个link.

+0

其无法运行它,它强调onActivityResult写:无效是一个无效的类型onActivityResult – rgreso

+0

变量你在错误的地方,即onActivityResult returnin您的视图V。它应该在onCreateView方法中。 –

0

您正在试图返回值时onActivityResult是无效的返回功能,你需要实现你的逻辑,而无需返回

2

好吧任何价值,你有没有很好地放置一个括号。我想你想`onActivityResult在点击监听器。

contactsButton.setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      if(v==contactsButton){ 
       Intent intent=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); 
       intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
       startActivityForResult(intent, 1); 
      } 
     } 
    }); 
// ^^ 
// This parenthesis should not be here 

删除括号和分号,并在这里添加它们:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      Uri ur = data.getData(); 
      Cursor c = managedQuery(ur, null, null, null, null); 
      if (c.moveToFirst()) { 
       String s = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       phoneNumberDisplay.setText(s); 
      } 
     } 
    }); 
// ^^ 
// Here, so that it is in the event listener 

注意:因为我固定在您的文章缩进我看到这个错误。下一次,请务必正确缩进代码,以便您能看到那种错误(这也是我不喜欢K支架风格的原因)。

更新: 这是Activity.RESULT_OK,不是裸RESULT_OK

对于managedQuery:见this question。对于getContentManager:见here

+0

感谢您的回答!是的,它修复了这个错误,但它并没有将选定的联系人插入到我的editText中,当我在另一个项目的Activity中有相同的代码时,它会插入它 – rgreso

+0

您可以评论'if(v == contactsButton)'(及其右括号)看看是否有效? (嗯,为什么在Java中的UI如此迟钝?) – Synxis

+0

对不起,我没有提到有另一个错误......它不知道RESULT_OK(在它的活动它),它想要实现managedQuery(在活动它doesnt)。它只在我删除*(resultCode == RESULT_OK)并创建未实现的managedQuery方法时才运行。对不起,我现在意识到了。我不知道如何implmenet managedQuery方法我真的是初学者,这个代码我发现在一些教程中,并在Activity中测试。 – rgreso

0
public String getPath(Uri uri) { 
    // just some safety built in 
    if(uri == null) { 
     // TODO perform some logging or show user feedback 
     return null; 
    } 
    // try to retrieve the image from the media store first 
    // this will only work for images selected from gallery 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Activity ac =new Activity(); 
    Cursor cursor =ac.managedQuery(uri, projection, null, null, null); 
    if(cursor != null){ 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    // this is our fallback here 
    return uri.getPath(); 
    } 
相关问题