【问题标题】:IntentBuilder(Context,ComponentName) has private access in IntentBuilderIntentBuilder(Context,ComponentName) 在 IntentBuilder 中具有私有访问权限
【发布时间】:2021-12-15 18:11:18
【问题描述】:

以下是我尝试运行应用程序时遇到的错误

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

new ShareCompat.IntentBuilder(...) 第一行的 Anonymous new View.OnClickListener(), onClick() 方法中。

当我浏览带有红色下划线的那条线时,Android Studio 会显示以下消息:

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

我正在开发 Android 版本 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();
            }
        });
    }
}

下面是AndroidManifest.xml的内容:

<?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>

下面是build.gradle (Module: Implicit_Intents:app)的内容:

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'
}

你能帮帮我吗,我被这个错误困住了?

谢谢。如果需要更多信息,请告诉我。

【问题讨论】:

    标签: java android android-intent android-11 android-implicit-intent


    【解决方案1】:

    使用公共构造函数:

    new ShareCompat.IntentBuilder(context)
                        .setType(mimeType)
                        .setChooserTitle("Share this text with:")
                        .setText(txt)
                        .startChooser();
    

    此处显示:https://developer.android.com/reference/androidx/core/app/ShareCompat.IntentBuilder#public-constructors


    您也可以直接从视图中获取上下文,而不是将其存储为字段:

    new ShareCompat.IntentBuilder(v.getContext())
                        .setType(mimeType)
                        .setChooserTitle("Share this text with:")
                        .setText(txt)
                        .startChooser();
    

    【讨论】:

    • new ShareCompat.IntentBuilder(context) required: Context,ComponentName found: 上下文原因:实际参数列表和正式参数列表的长度不同
    • 它给了我以下错误,如上面的评论所示
    • 更新了答案。
    • 您使用的是哪个版本的 AndroidX?检查是否有更新
    • 我正在使用 Android Studio 北极狐 | 2020.3.1补丁3。我检查了更新,没有。
    【解决方案2】:

    专业提示:Android 开源。如果遇到此类问题,可以直接跳转到类定义看看有什么问题。

    在这种情况下,通过查看 ShareCompat 源代码,您会发现虽然构造函数是私有的,但有一个 from 静态构建器方法。

    奇怪的是,这在文档中被标记为已弃用,所以它似乎是一个文档错误:

    即使文档说要使用构造函数,它也不起作用:

    from 静态构造函数可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 2023-03-23
      • 2014-11-24
      • 1970-01-01
      • 2016-05-23
      • 2020-05-07
      • 2020-03-01
      相关资源
      最近更新 更多