Java 添加ImageView会使android应用程序小部件崩溃

Java 添加ImageView会使android应用程序小部件崩溃,java,android,imageview,widget,Java,Android,Imageview,Widget,我正在制作一个非常简单的小部件,它使用ImageView显示图像 我的布局如下所示 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dip"> <!--

我正在制作一个非常简单的小部件,它使用ImageView显示图像

我的布局如下所示

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dip">

    <!-- ImageView logo -->
    <ImageView
        android:id="@+id/logo"
        android:src="@drawable/giants"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitXY" />

</RelativeLayout>
 public class LogoWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager 
    appWidgetManager, int[] appWidgetIds) {

        // What Happens While App Goes Through Each Widget Id//
        for (int currentWidgetId: appWidgetIds) {

            // Define And Instantiate Variable RemoteViews widgetLogo//
            RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);

            // Update Widget//
            appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);
        }
    }
}
    <!-- Widget Provider Logo -->
    <receiver android:name=".LogoWidgetProvider">

        <!-- Tell The App That There Is A Widget To Display -->
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <!-- Tell The App Where To Find Widget Data -->
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info_logo">
        </meta-data>

    </receiver>
<!-- Basic Information For Widget Logo -->
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_logo"
    android:minHeight="110dip"
    android:minWidth="110dip"
    android:minResizeWidth="40dip"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="3600000"
    android:widgetCategory="home_screen">

</appwidget-provider>
我清单中的接收者如下所示

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dip">

    <!-- ImageView logo -->
    <ImageView
        android:id="@+id/logo"
        android:src="@drawable/giants"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitXY" />

</RelativeLayout>
 public class LogoWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager 
    appWidgetManager, int[] appWidgetIds) {

        // What Happens While App Goes Through Each Widget Id//
        for (int currentWidgetId: appWidgetIds) {

            // Define And Instantiate Variable RemoteViews widgetLogo//
            RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);

            // Update Widget//
            appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);
        }
    }
}
    <!-- Widget Provider Logo -->
    <receiver android:name=".LogoWidgetProvider">

        <!-- Tell The App That There Is A Widget To Display -->
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <!-- Tell The App Where To Find Widget Data -->
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info_logo">
        </meta-data>

    </receiver>
<!-- Basic Information For Widget Logo -->
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_logo"
    android:minHeight="110dip"
    android:minWidth="110dip"
    android:minResizeWidth="40dip"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="3600000"
    android:widgetCategory="home_screen">

</appwidget-provider>

最后,小部件提供程序XML如下所示

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dip">

    <!-- ImageView logo -->
    <ImageView
        android:id="@+id/logo"
        android:src="@drawable/giants"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitXY" />

</RelativeLayout>
 public class LogoWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager 
    appWidgetManager, int[] appWidgetIds) {

        // What Happens While App Goes Through Each Widget Id//
        for (int currentWidgetId: appWidgetIds) {

            // Define And Instantiate Variable RemoteViews widgetLogo//
            RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);

            // Update Widget//
            appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);
        }
    }
}
    <!-- Widget Provider Logo -->
    <receiver android:name=".LogoWidgetProvider">

        <!-- Tell The App That There Is A Widget To Display -->
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <!-- Tell The App Where To Find Widget Data -->
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info_logo">
        </meta-data>

    </receiver>
<!-- Basic Information For Widget Logo -->
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_logo"
    android:minHeight="110dip"
    android:minWidth="110dip"
    android:minResizeWidth="40dip"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="3600000"
    android:widgetCategory="home_screen">

</appwidget-provider>

出于某种原因,小部件每次都会崩溃。如果我将ImageView切换到Button,它可以正常工作。让小部件显示图像缺少什么


谢谢

我想明白了。无论分辨率如何,您都可以使用任何图像,但它需要放在一个400像素×400像素的框中。我用油漆垫做的。一旦它进入框中,图像就会显示出来

你也可以发布堆栈跟踪吗?@MykhailoYuzheka堆栈跟踪中没有显示任何内容。应用程序本身不会崩溃,只是当我在主页上输入时,小部件会崩溃。你应该用你的问题的标题来描述问题。这样,更有经验的开发人员更容易发现他们专业领域中的问题。你也应该试着问一些简短的问题,因为他们会更快地得到答案,而且答案的质量也会更好