android–AppCompat打破Launcher Widget的能力. “找不到任何视图,使用错误视图”

android–AppCompat打破Launcher Widget的能力. “找不到任何视图,使用错误视图”,第1张

概述我是一个启动器开发人员,因为小部件的基础始终遵循我发现的这个简单指南:HostingAndroidWidgets.现在这个方法在使用演示应用程序时可以100%工作.问题是我一添加AppCompat主题并扩展AppCompatActivity而不是Activity使用最新的com.android.support:appcompat-v7:25.1.1,我遇到了问

我是一个启动器开发人员,因为小部件的基础始终遵循我发现的这个简单指南:Hosting Android Widgets.现在这个方法在使用演示应用程序时可以100%工作.问题是我一添加AppCompat主题并扩展AppCompatActivity而不是Activity
使用最新的com.androID.support:appcompat-v7:25.1.1,我遇到了问题.从ACTION_APPWidget_PICK对话框中选择某些小部件后,我得到如下错误

W/AppWidgetHostVIEw: updateAppWidget Couldn't find any vIEw, using error vIEw androID.Widget.RemoteVIEws$ActionException: vIEw: androID.support.v7.Widget.AppCompatimageVIEw can't use method with RemoteVIEws: setimageResource(int)at androID.Widget.RemoteVIEws.getmethod(RemoteVIEws.java:775)at androID.Widget.RemoteVIEws.access0(RemoteVIEws.java:69)at androID.Widget.RemoteVIEws$ReflectionAction.apply(RemoteVIEws.java:1266)at androID.Widget.RemoteVIEws.performApply(RemoteVIEws.java:2587)at androID.Widget.RemoteVIEws.apply(RemoteVIEws.java:2547)at androID.appWidget.AppWidgetHostVIEw.updateAppWidget(AppWidgetHostVIEw.java:395)at androID.appWidget.AppWidgetHost.createVIEw(AppWidgetHost.java:336)at com.lgfischer.Widgethost.WidgetHostExampleActivity.createWidget(WidgetHostExampleActivity.java:129)at com.lgfischer.Widgethost.WidgetHostExampleActivity.onActivityResult(WidgetHostExampleActivity.java:93)at androID.app.Activity.dispatchActivityResult(Activity.java:6168)at androID.app.ActivityThread.deliverResults(ActivityThread.java:3732)at androID.app.ActivityThread.handleSendResult(ActivityThread.java:3779)at androID.app.ActivityThread.access00(ActivityThread.java:162)at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)at androID.os.Handler.dispatchMessage(Handler.java:106)at androID.os.Looper.loop(Looper.java:189)at androID.app.ActivityThread.main(ActivityThread.java:5529)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:745)

此外,AppWidgetHostVIEw显示“无法添加小部件”.基于错误日志,人们自然会看一下WidgetHostExampleActivity.java第129行,但这只是调用mAppWidgetHost.createVIEw(this,appWidgetID,appWidgetInfo);.

似乎代码可以在appcompat-v7:22.1.1上运行,但不会超过它.

注意:

>我已经在AndroID 5.0.2和AndroID 7.1.1上测试并确认了此错误
>这只发生在一些小部件上.一些失败的是AndroID 7.1.1和Play – 我的图书馆游戏商店小部件上的默认日历应用.
>在Play商店的实际启动器中,我创建了一个自定义窗口小部件选择活动,但仍然存在所描述的问题

我有这个项目的完整源代码,我在上面提到了修改:Google Drive

这是完整的WidgetHostExampleActivity:

public class WidgetHostExampleActivity extends AppCompatActivity{    final static int APPWidget_HOST_ID = 111;    final static int REQUEST_PICK_APPWidget = 222;    final static int REQUEST_CREATE_APPWidget = 333;    static final String TAG = "WidgetHostExampleActivity";    AppWidgetManager mAppWidgetManager;    AppWidgetHost mAppWidgetHost;    VIEwGroup mainlayout;    /**     * Called on the creation of the activity.     */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        mainlayout = (VIEwGroup) findVIEwByID(R.ID.main_layout);        mAppWidgetManager = AppWidgetManager.getInstance(this);        mAppWidgetHost = new AppWidgetHost(this, APPWidget_HOST_ID);    }    /**     * Launches the menu to select the Widget. The selected Widget will be on     * the result of the activity.     */    voID selectWidget() {        int appWidgetID = this.mAppWidgetHost.allocateAppWidgetID();        Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWidget_PICK);        pickIntent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetID);        addEmptyData(pickIntent);        startActivityForResult(pickIntent, REQUEST_PICK_APPWidget);    }    /**     * This avoIDs a BUG in the com.androID.settings.AppWidgetPickActivity,     * which is used to select Widgets. This just adds empty extras to the     * intent, avoIDing the BUG.     *      * See more: http://code.Google.com/p/androID/issues/detail?ID=4272     */    voID addEmptyData(Intent pickIntent) {        ArrayList<appwidgetproviderInfo> customInfo = new ArrayList<appwidgetproviderInfo>();        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);        ArrayList<Bundle> customExtras = new ArrayList<Bundle>();        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);    }    /**     * If the user has selected an Widget, the result will be in the 'data' when     * this function is called.     */    @OverrIDe    protected voID onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESulT_OK) {            if (requestCode == REQUEST_PICK_APPWidget) {                configureWidget(data);            } else if (requestCode == REQUEST_CREATE_APPWidget) {                createWidget(data);            }        } else if (resultCode == RESulT_CANCELED && data != null) {            int appWidgetID = data.getIntExtra(AppWidgetManager.EXTRA_APPWidget_ID, -1);            if (appWidgetID != -1) {                mAppWidgetHost.deleteAppWidgetID(appWidgetID);            }        }    }    /**     * Checks if the Widget needs any configuration. If it needs, launches the     * configuration activity.     */    private voID configureWidget(Intent data) {        Bundle extras = data.getExtras();        int appWidgetID = extras.getInt(AppWidgetManager.EXTRA_APPWidget_ID, -1);        appwidgetproviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetID);        if (appWidgetInfo.configure != null) {            Intent intent = new Intent(AppWidgetManager.ACTION_APPWidget_CONfigURE);            intent.setComponent(appWidgetInfo.configure);            intent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetID);            startActivityForResult(intent, REQUEST_CREATE_APPWidget);        } else {            createWidget(data);        }    }    /**     * Creates the Widget and adds to our vIEw layout.     */    public voID createWidget(Intent data) {        Bundle extras = data.getExtras();        int appWidgetID = extras.getInt(AppWidgetManager.EXTRA_APPWidget_ID, -1);        appwidgetproviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetID);        AppWidgetHostVIEw hostVIEw = mAppWidgetHost.createVIEw(this, appWidgetID, appWidgetInfo);        hostVIEw.setAppWidget(appWidgetID, appWidgetInfo);        mainlayout.addVIEw(hostVIEw);        Log.i(TAG, "The Widget size is: " + appWidgetInfo.minWIDth + "*" + appWidgetInfo.minHeight);    }    /**     * Registers the AppWidgetHost to Listen for updates to any Widgets this app     * has.     */    @OverrIDe    protected voID onStart() {        super.onStart();        mAppWidgetHost.startListening();    }    /**     * Stop Listen for updates for our Widgets (saving battery).     */    @OverrIDe    protected voID onStop() {        super.onStop();        mAppWidgetHost.stopListening();    }    /**     * Removes the Widget displayed by this AppWidgetHostVIEw.     */    public voID removeWidget(AppWidgetHostVIEw hostVIEw) {        mAppWidgetHost.deleteAppWidgetID(hostVIEw.getAppWidgetID());        mainlayout.removeVIEw(hostVIEw);    }    /**     * Handles the menu.     */    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        Log.i(TAG, "Menu selected: " + item.getTitle() + " / " + item.getItemID() + " / " + R.ID.addWidget);        switch (item.getItemID()) {        case R.ID.addWidget:            selectWidget();            return true;        case R.ID.removeWidget:            removeWidgetMenuSelected();            return false;        }        return super.onoptionsItemSelected(item);    }    /**     * Handle the 'Remove Widget' menu.     */    public voID removeWidgetMenuSelected() {        int childCount = mainlayout.getChildCount();        if (childCount > 1) {            VIEw vIEw = mainlayout.getChildAt(childCount - 1);            if (vIEw instanceof AppWidgetHostVIEw) {                removeWidget((AppWidgetHostVIEw) vIEw);                Toast.makeText(this, R.string.Widget_removed_popup, Toast.LENGTH_SHORT).show();                return;            }        }        Toast.makeText(this, R.string.no_Widgets_popup, Toast.LENGTH_SHORT).show();    }    /**     * Creates the menu with options to add and remove Widgets.     */    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        MenuInflater inflater = getMenuInflater();        inflater.inflate(R.menu.Widget_menu, menu);        return true;    }}

这是我的settings.gradel:

apply plugin: 'com.androID.application'androID {    compileSdkVersion 25    buildToolsversion "25.0.2"    defaultConfig {        applicationID "com.lgfischer.Widgethost"        minSdkVersion 11        targetSdkVersion 22    }    buildTypes {        release {            MinifyEnabled false            shrinkResources false            proguardfiles getDefaultProguardfile('proguard-androID.txt'), 'proguard-rules.txt'        }    }}dependencIEs {    compile 'com.androID.support:appcompat-v7:25.1.1'}

PS:我现在几乎没有发布问题,所以当我这样做时,你知道这是一个真正的问题.

解决方法:

将管理器和主机的上下文更改为应用程序上下文而不是活动上下文.

AppWidgetManager = AppWidgetManager.getInstance(getApplicationContext());mAppWidgetHost = new AppWidgetHost(getApplicationContext(), APPWidget_HOST_ID);

刚刚为我解决了这个问题.

总结

以上是内存溢出为你收集整理的android – AppCompat打破Launcher Widget的能力. “找不到任何视图,使用错误视图”全部内容,希望文章能够帮你解决android – AppCompat打破Launcher Widget的能力. “找不到任何视图,使用错误视图”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存