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>