android - widget help -


i've trying practice making battery widget shows current battery status.

i've typed source code :

the java file :

package cam.widget.batterywiz;  import android.app.service; import android.appwidget.appwidgetmanager; import android.appwidget.appwidgetprovider; import android.content.componentname; import android.content.context; import android.content.intent; import android.os.ibinder; import android.widget.remoteviews;  public class batwiz extends appwidgetprovider {     /** called when activity first created. */     @override     public void onupdate (context c, appwidgetmanager awm, int[] id) {         c.startservice(new intent (c, updateservice.class));     }      public static class updateservice extends service {         public void onstart (intent intent, int startid) {             remoteviews rv = new remoteviews (getpackagename(), r.layout.bat);             rv.settextviewtext(r.id.battery, "80%");             componentname thiswidget = new componentname (this, batwiz.class);             appwidgetmanager m = appwidgetmanager.getinstance(this);             m.updateappwidget(thiswidget, rv);         }           @override         public ibinder onbind(intent intent) {             // todo auto-generated method stub             return null;         }      } } 

bat2.xml file :

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/widget"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical"     >      <imageview         android:id="@+id/battery"         android:src="@drawable/battery_icon"         android:croptopadding="true"     >     </imageview> </linearlayout> 

android manifest :

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="cam.widget.batterywiz"       android:versioncode="1"       android:versionname="1.0">     <application android:icon="@drawable/icon" android:label="@string/app_name">          <receiver android:name=".batwiz">             <intent-filter>                 <action android:name="android.appwidget.action.appwidget_update" />             </intent-filter>             <meta-data android:name="android.appwidget.provider"                        android:resource="@layout/bat" />         </receiver>      </application>     <uses-sdk android:minsdkversion="4" />  </manifest>  

can guys try figure out mistake ??
because in emulator says "problem loading widget"
thx help..

the problem in manifest file, says @xml/bat.

and provider info should in /res/xml folder.

also, you'll need change remoteviews creation remoteviews rv = new remoteviews (getpackagename(), r.layout.bat2);,

and imageviews don't have settext method.

edit:

changes

remoteviews rv = new remoteviews (getpackagename(), r.layout.bat2);  android:resource="@xml/bat" /> 

(and move bat.xml layout/ xml/)

rv.settextviewtext(r.id.battery, "80%"); 

this not work, imageview not child of textview , doesn't have settext method.


Comments