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>

Maxim
  • 3
  • 1
  • 4

3 Answers3

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.

dsinczak
  • 217
  • 1
  • 10
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);

Community
  • 1
  • 1
mrsus
  • 5,446
  • 4
  • 31
  • 44
  • Thanx a lot! I was trying to use RemoteView unsuccessfully... I used setImageIiewBitmap for RemoteView instead of web.setImageBitmap(BitmapFactory.decodeStream(is)) and I changed ImageView to RemoteView in activity_main.xml. OK, my question is Why "ImageView web = (ImageView)findViewById (R.id.Widget);" does not work even if I delete all code lines internet related? E.g. if i use local image. ImageView is allowed in widgets, isn't it? Sorry for my stupid questions! :)) – Maxim Oct 11 '13 at 07:21
  • your welcome, :), if this answer was helpful to you then mark it as right answer. – mrsus Oct 11 '13 at 07:22
  • because `findViewById` is part of `Activity` class, not `AppWidgetProvider`. i.e. you extended your class with `AppWidgetProvider` – mrsus Oct 11 '13 at 07:26
  • `android.app.Activity.findViewById(int id)` returns `View` which was processed in `onCreate()` method of `Activity`. – mrsus Oct 11 '13 at 07:32
  • Ok, thanx again!! But if ImageView is allowed in widget so could you give me example what should i use instead of findViewById? – Maxim Oct 11 '13 at 07:42
  • And in this case I should use RemoteView instead of ImageView in activity_main? – Maxim Oct 11 '13 at 08:00
  • getPackageName() is not allowed in AppWidgetProvider... "It works if I call from a method to which a View or an Activity object is available, but if I want to find the package name from a totally independent class with no View or Activity..." it does not work [link](http://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere) – Maxim Oct 11 '13 at 08:46
-1

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

Kristijan Drača
  • 614
  • 4
  • 16