Android Build Error:不兼容的类型:Object无法转换为ResolveInfo

问题描述 投票:0回答:1

我是Android开发的新手,我正在尝试根据https://android-developers.googleblog.com/2015/10/in-app-translations-in-android.html将菜单项追加到Cut / Copy / Paste上下文菜单中。我知道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
android
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.