Java onActivityResult未被覆盖,且从未在按钮中到达。单击侦听器
我会开始说,我只是希望这不是一件愚蠢到我无法注意到的事情,但是经过几个小时的研究,我还没有找到我的答案。在我的Java onActivityResult未被覆盖,且从未在按钮中到达。单击侦听器,java,android,android-contacts,onactivityresult,Java,Android,Android Contacts,Onactivityresult,我会开始说,我只是希望这不是一件愚蠢到我无法注意到的事情,但是经过几个小时的研究,我还没有找到我的答案。在我的SMSActivity中,我创建了一个按钮cmdApriRubrica,该按钮打开联系人默认应用程序,然后在选择联系人时,从理论上将其数据返回到我的活动中(我已经查阅了)。“联系人”应用程序可以正常打开,但问题是我从未得到结果。我的代码如下: ((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Butto
SMSActivity
中,我创建了一个按钮cmdApriRubrica
,该按钮打开联系人默认应用程序,然后在选择联系人时,从理论上将其数据返回到我的活动中(我已经查阅了)。“联系人”应用程序可以正常打开,但问题是我从未得到结果。我的代码如下:
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 200);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(int requestCode, int resultCode, Intent data); CAN'T CALL THIS
// Check which request it is that we're responding to
if (requestCode == 200 && resultCode == RESULT_OK) {
// Make sure the request was successful
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numero = cursor.getString(column);
Log.i("Dati contatto: ","Nome = "+nome+", numero = "+numero);
// Do something with the phone number...
txtDati.setText(new StringBuilder().append(nome).append("\t------------\t").append(numero).toString()); //Android Studio suggested me doing this...
}
}
});
TextView txtDati = null; //This is initialized just below the class declaration to have it visible
public static final int PICK_CONTACT_REQ_CODE = 200;
...
findViewById(R.id.cmdApriRubrica).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQ_CODE);
}
});
...
//At the same level of the onCreate() method, as I've been told!
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == 200 && resultCode == RESULT_OK && txtDati != null) {
// Make sure the request was successful
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numero = cursor.getString(column);
Log.i("Dati contatto: ","Nome = "+nome+", numero = "+numero);
// Do something with the phone number...
txtDati.setText(new StringBuilder().append(nome).append("\t------------\t").append(numero).toString()); //Android Studio suggested me doing this...
}
else
Log.i("Problem ActivityResult","Something wrong in onActivityResult method!");
}
我认为主要的问题是我参加了onClick()
活动:我一直在寻找答案、答案、bug、答案、站点和其他投票率较低的答案。许多人建议调用super.onActivityResult
,但它找不到方法,所以我不能调用它。另外,Android Studio告诉我,我在那里编写的onActivityResult不会覆盖其超类的方法。为什么会发生这种情况?关于这件事,我还没有找到任何答案。如果这是问题所在,是否有办法避免在onClick
方法中写入此内容,或者我是否做错了无法找到解决方案的事情?谢谢你抽出时间。
编辑:我已经按照建议修改了代码;我的代码如下:
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 200);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(int requestCode, int resultCode, Intent data); CAN'T CALL THIS
// Check which request it is that we're responding to
if (requestCode == 200 && resultCode == RESULT_OK) {
// Make sure the request was successful
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numero = cursor.getString(column);
Log.i("Dati contatto: ","Nome = "+nome+", numero = "+numero);
// Do something with the phone number...
txtDati.setText(new StringBuilder().append(nome).append("\t------------\t").append(numero).toString()); //Android Studio suggested me doing this...
}
}
});
TextView txtDati = null; //This is initialized just below the class declaration to have it visible
public static final int PICK_CONTACT_REQ_CODE = 200;
...
findViewById(R.id.cmdApriRubrica).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQ_CODE);
}
});
...
//At the same level of the onCreate() method, as I've been told!
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == 200 && resultCode == RESULT_OK && txtDati != null) {
// Make sure the request was successful
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String numero = cursor.getString(column);
Log.i("Dati contatto: ","Nome = "+nome+", numero = "+numero);
// Do something with the phone number...
txtDati.setText(new StringBuilder().append(nome).append("\t------------\t").append(numero).toString()); //Android Studio suggested me doing this...
}
else
Log.i("Problem ActivityResult","Something wrong in onActivityResult method!");
}
我现在遇到了这个异常,但我无法理解Cursor.query是如何工作的:
java.lang.RuntimeException:未能传递结果ResultInfo{who=null,request=200,result=-1,data=Intent{dat=content://com.android.contacts/data/3045 flg=0x1}}到活动{com.fun.is.activity.gguiduzzi.orariobello/com.fun.is.activity.gguiduzzi.orariobello.SMSActivity}:java.lang.IllegalStateException:无法读取第0行,光标窗口中的列-1。在从光标访问数据之前,请确保光标已正确初始化。
谁能告诉我哪里出了问题?我一直在努力寻找它。谢谢大家! 在我看来,这里有两个问题。您正在使用OnClickListener from按钮的方法。您应该使用视图
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Button.OnClickListener() { WRONG
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new View.OnClickListener() { OK
您遇到的第二个问题是,您正在覆盖de按钮侦听器内部的onActivityResult,而不是活动,这就是为什么您不能使用super.onActivityResult。
您应该将此代码(onActivityResult)置于onCreate、onDestroy等的同一级别。因此我应该在外部重写它,谢谢!你能解释一下使用Button.OnClickListener查看时有什么变化吗?@njzk2可能是错误的,这里的词不正确,是我的错,我应该使用“可能的变化”之类的词。他在寻找解决问题的办法,我回答了我认为他可以改变什么来解决这个问题。在API()和我参与的大多数项目中,使用Button.OnClickListener,这就是我建议使用Button.OnClickListener的原因。