0

I've got "...findViewById is undefined..." error here in my super code :( This code should make a Home screen widget and the one should be updated every hour with an image from Inet. (The source image is changed one time per 3 hours on server). Help plz!

MainActivity.java:

    package ru.spaceballssoftware.magneticstorm;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.graphics.BitmapFactory;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ImageView;
    import android.widget.RemoteViews;


    public class MainActivity extends AppWidgetProvider
    {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,         int[] appWidgetIds) {

         ImageView web = (ImageView)findViewById (R.id.Widget);
    try {
        URL myFileUrl = new URL ("http://geo-storm.ru/Kp.gif");
        HttpURLConnection conn =
          (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();

        InputStream is = conn.getInputStream();
        web.setImageBitmap(BitmapFactory.decodeStream(is));

        return;

      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }

      return;

    }

    }

layout/activity_main.xml

   <ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

     />

res/xml/manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dp"
    android:minHeight="146dp"
    android:updatePeriodMillis="3600000"
    android:initialLayout="@layout/activity_main">
    </appwidget-provider>

AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"

          package="ru.spaceballssoftware.magneticstorm"
          android:versionCode="1"
          android:versionName="1.0">

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.INTERNET"/>
        <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:allowBackup="true" android:permission="android.permission.INTERNET">


            <receiver android:name="MagneticStorm" >
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>
                <meta-data android:name="android.appwidget.provider" android:resource="@xml/manifest" />
    </receiver>
    <!--конец-->

</application>

4

3 回答 3

1

First of all I would separate image download and image change. E.g. image download can be done in intentservice which is scheduled in application and executed by timer. This image can be saved in some place (and e.g. broadcast about the fact can be send). Image change is done in AppWidgetProvider but from local place which is safer.

于 2013-10-10T10:39:16.590 回答
0

You can never reference or modify views in Appwidget directly. You should do this in RemoteViews.

[EDIT]:

 remoteViews.setImageViewBitmap(R.id.yourid, bitmap);

moew info here

RemoteViews views = new RemoteViews(getPackageName(), R.layout.yourlayout);

于 2013-10-10T10:32:30.960 回答
-1

try using this library made for images from the web http://loopj.com/android-smart-image-view/

于 2013-10-11T07:56:37.667 回答