onActivityResult 未被覆盖并且从未在 Button.ClickListener 内到达
onActivityResult not being Overridden and never reached in a Button.ClickListener
我会开始说我只是希望这不是愚蠢到我没有注意到它,但经过几个小时的研究我还没有找到我的答案。在我的SMSActivity
中,我创建了一个Button cmdApriRubrica
,它打开了contacts默认应用程序,然后选择联系人returns的数据到我的activity,理论上是从理论上看这在 Android Developer Basic Trainings)。联系人应用程序可以正常打开,但问题是我从来没有得到结果。我的代码如下:
((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...
}
}
});
我认为主要问题是我在 onClick()
事件中:我已经寻找 this answer, this answer, this bug, this answer, this 网站和其他一些投票较少的答案。许多人建议调用 super.onActivityResult
但它找不到方法,所以我无法调用它。此外,Android Studio 告诉我,我在那里编写的 onActivityResult 不会覆盖其超类的方法。为什么会这样?我还没有找到任何答案。有没有办法避免在 onClick
方法中写这个,如果这是问题所在,或者我做错了什么我找不到解决方案?感谢您的时间。
编辑:我已经按照建议修改了代码;我的代码现在如下:
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 }}至 activity {com.fun.is.activity.gguiduzzi.orariobello/com.fun.is.activity.gguiduzzi.orariobello.SMSActivity}:java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,第 -1 列。在访问 Cursor 中的数据之前,请确保 Cursor 已正确初始化。
有人可以告诉我问题出在哪里吗?我一直在努力寻找它。谢谢!
我认为你这里有两个问题。您正在使用 Button 的 OnClickListener 方法。你应该使用视图。
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Button.OnClickListener() { WRONG
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new View.OnClickListener() { OK
你遇到的第二个问题是你覆盖了按钮监听器中的 onActivityResult 而不是 activity 这就是为什么你不能使用 super.onActivityResult.
你应该把这段代码(onActivityResult)和onCreate、onDestroy等放在同一个层次
我会开始说我只是希望这不是愚蠢到我没有注意到它,但经过几个小时的研究我还没有找到我的答案。在我的SMSActivity
中,我创建了一个Button cmdApriRubrica
,它打开了contacts默认应用程序,然后选择联系人returns的数据到我的activity,理论上是从理论上看这在 Android Developer Basic Trainings)。联系人应用程序可以正常打开,但问题是我从来没有得到结果。我的代码如下:
((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...
}
}
});
我认为主要问题是我在 onClick()
事件中:我已经寻找 this answer, this answer, this bug, this answer, this 网站和其他一些投票较少的答案。许多人建议调用 super.onActivityResult
但它找不到方法,所以我无法调用它。此外,Android Studio 告诉我,我在那里编写的 onActivityResult 不会覆盖其超类的方法。为什么会这样?我还没有找到任何答案。有没有办法避免在 onClick
方法中写这个,如果这是问题所在,或者我做错了什么我找不到解决方案?感谢您的时间。
编辑:我已经按照建议修改了代码;我的代码现在如下:
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 }}至 activity {com.fun.is.activity.gguiduzzi.orariobello/com.fun.is.activity.gguiduzzi.orariobello.SMSActivity}:java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,第 -1 列。在访问 Cursor 中的数据之前,请确保 Cursor 已正确初始化。
有人可以告诉我问题出在哪里吗?我一直在努力寻找它。谢谢!
我认为你这里有两个问题。您正在使用 Button 的 OnClickListener 方法。你应该使用视图。
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new Button.OnClickListener() { WRONG
((Button)findViewById(R.id.cmdApriRubrica)).setOnClickListener( new View.OnClickListener() { OK
你遇到的第二个问题是你覆盖了按钮监听器中的 onActivityResult 而不是 activity 这就是为什么你不能使用 super.onActivityResult.
你应该把这段代码(onActivityResult)和onCreate、onDestroy等放在同一个层次