I want to create a function that takes data from contacts and registers it as a friend. However, there is no problem with pressing the Register with your contact button and showing the registered contact on your phone to the screen where you can choose, but the app ends or turns into a black screen as soon as you press the contact you want to register.
I'd like to know the cause.
The code below is the activity code. It looks too long, so I excluded the package and import.
public class AddFriendsActivity extends AppCompatActivity {
String name = "";
String number = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friends);
Button registerWithPhoneNumberButton = (Button) findViewById(R.id.registerWithPhoneNumberButton);
registerWithPhoneNumberButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(v.getContext(), EnteringInformationOfFriendActivity.class);
startActivity(intent);
}
});
Button registerWithAddressBookButton = (Button) findViewById(R.id.registerWithAddressBookButton);
registerWithAddressBookButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// Contact Selection Screen
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Button registerWithKakaoButton = (Button) findViewById(R.id.registerWithKakaoButton);
registerWithKakaoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// Link to Kakao Talk
}
});
EditText nameTestEdit = (EditText) findViewById(R.id.nameTestEdit);
nameTestEdit.setText(name);
EditText numTestEdit = (EditText) findViewById(R.id.numTestEdit);
numTestEdit.setText(number);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK)
{
Cursor cursor = getContentResolver().query(data.getData(),
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
cursor.moveToFirst();
name = cursor.getString(0); // 0 -> Name
number = cursor.getString(1); // 1 -> Number
cursor.close();
}
// Appears to stop without going through here
if(resultCode == RESULT_CANCELED)
{
Toast failToast = Toast.makeText(this.getApplicationContext(), "Failed to load contact", Toast.LENGTH_SHORT);
Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
failToast.show();
}
}, 500); // show failToast after 0.5 seconds
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Just in case you need it, the xml...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tempus.ui.friends.AddFriendsActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_gravity="center|left"
android:layout_marginLeft="20dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="Register an acquaintance"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/layoutborder4">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
Android:text="Choose your preferred registration method."/>
<Button
android:id="@+id/registerWithPhoneNumberButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="Register by phone number"/>
<Button
android:id="@+id/registerWithAddressBookButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="Register as a contact"/>
<Button
android:id="@+id/registerWithKakaoButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="Register via Kakao Talk"/>
<EditText
android:id="@+id/nameTestEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="test"/>
<EditText
android:id="@+id/numTestEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="test"/>
</LinearLayout>
</LinearLayout>
// Appears to stop without passing through here
There was no choice but to say that there an error accessing the cursor. There's a sense of approaching the cursor too carefully. The moveToFirst() method returns a boolean value, which means that the cursor is empty if it is false. I think you should wrap it around the if statement and check if the data is being transferred well.
If the data doesn't come through well, there might be a problem with the projection part.
getContentResolver().query(data.getData(), null, null, null, null);
By nullifying the projection, search all the data to see if there is data in the cursor or not.
© 2024 OneMinuteCode. All rights reserved.