Search code examples
javaandroidandroid-intentandroid-11android-implicit-intent

IntentBuilder(Context,ComponentName) has private access in IntentBuilder


The following is the error I get when I try running the application

error: constructor IntentBuilder in class IntentBuilder cannot be applied to given types;
                new ShareCompat.IntentBuilder(context)
                ^
  required: Context,ComponentName
  found: Context
  reason: actual and formal argument lists differ in length

within the Anonymous new View.OnClickListener(), onClick() method at the first line of new ShareCompat.IntentBuilder(...).

When I surf above that line which have red curly underline, Android Studio shows the following message:

'IntentBuilder(android.content.Context, android.content.ComponentName)' has private access in 'androidx.core.app.ShareCompat.IntentBuilder'

I am working on Android Version API 30: Andorid 11.0 (R).

package com.example.implicitintents;

import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ShareCompat;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private Context context = this;
    private Button openShareBtn;
    private EditText shareEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        openShareBtn = findViewById(R.id.open_share_button);
        shareEditText = findViewById(R.id.share_edittext);

        openShareBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = shareEditText.getText().toString();
                String mimeType = "text/plain";
                new ShareCompat.IntentBuilder(context)
                        .setType(mimeType)
                        .setChooserTitle("Share this text with: ")
                        .setText(txt)
                        .startChooser();
            }
        });
    }
}

Below is the AndroidManifest.xml content:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.implicitintents">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ImplicitIntents">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="*" />
        </intent>
    </queries>

</manifest>

Below is the build.gradle (Module: Implicit_Intents:app) content:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.implicitintents"
        minSdk 30
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Can you please help me out here, I am stuck with this error?

Thank you. If more information is required please do let me know.


Solution

  • Pro Tip: Android in open source. If you run into this kind of issue, you can jump to the class definition and see what's wrong.

    In this case, by looking at the ShareCompat source, you find that while the constructor is private, there is a from static builder method.

    Strangely, this is marked as deprecated in the documentation, so it seems like a documentation bug:

    Constructors don't work even though the documentation says to use it:

    enter image description here

    The from static constructor is available.

    enter image description here