Android Build错误:类型不兼容:无法将对象转换为ResolveInfo

时间:2019-01-02 20:08:20

标签: android

我是Android开发的新手,正在尝试根据https://android-developers.googleblog.com/2015/10/in-app-translations-in-android.html在全球范围内将菜单项附加到“剪切/复制/粘贴”上下文菜单中。我知道getSupportedActivities()返回的对象无法与ResolveInfo兼容,但是不确定要采取什么措施来纠正此问题。

我的AndroidManifest.xml

中包含以下内容
<activity
    android:name=".ProcessTextActivity"
    android:label="@string/process_text">
    <intent-filter>
      <action android:name="android.intent.action.PROCESS_TEXT" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

这在ProcessTextActivity.java

package com.my-app;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.List;

public class ProcessTextActivity extends Activity {
    TextView mTextView;

    private Intent createProcessTextIntent() {
        return new Intent()
                .setAction(Intent.ACTION_PROCESS_TEXT)
                .setType("text/plain");
    }

    private List getSupportedActivities() {
        PackageManager packageManager =
                mTextView.getContext().getPackageManager();
        return packageManager.queryIntentActivities(createProcessTextIntent(), 0);
    }

    public void onInitializeMenu(Menu menu) {
        // Start with a menu Item order value that is high enough
        // so that your "PROCESS_TEXT" menu items appear after the
        // standard selection menu items like Cut, Copy, Paste.
        int menuItemOrder = 100;
        for (ResolveInfo resolveInfo : getSupportedActivities()) {
            menu.add(
                    Menu.NONE,
                    Menu.NONE,
                    menuItemOrder++,
                    getLabel(resolveInfo))
                    .setIntent(createProcessTextIntentForResolveInfo(resolveInfo))
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
    }

    private Intent createProcessTextIntentForResolveInfo(ResolveInfo info) {
        return createProcessTextIntent()
                .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, !
                        mTextView.onCheckIsTextEditor())
                .setClassName(info.activityInfo.packageName,
                        info.activityInfo.name);
    }
}

当我尝试构建时,出现以下错误:

ProcessTextActivity.java:33: error: incompatible types: Object cannot be converted to ResolveInfo
    for (ResolveInfo resolveInfo : getSupportedActivities()) {
                                                         ^
ProcessTextActivity.java:38: error: cannot find symbol
                getLabel(resolveInfo))
                ^

  symbol:   method getLabel(ResolveInfo)
  location: class ProcessTextActivity
  2 errors

1 个答案:

答案 0 :(得分:1)

似乎packageManager.queryIntentActivities(createProcessTextIntent(), 0)返回List<ResolveInfo>(我在android studio中检查了反编译的类PackageManager类),这意味着您可以使用:

 private List<ResolveInfo> getSupportedActivities() {
        PackageManager packageManager =
                mTextView.getContext().getPackageManager();
        return packageManager.queryIntentActivities(createProcessTextIntent(), 0);
    }

通过使用上面的代码,您将不再收到错误,希望对您有所帮助