Android第一个应用程序/小部件 - Log Cat

时间:2013-04-09 15:40:55

标签: android widget

昨天我开始在Eclipse中编程到Android,我做了简单的程序(Hello world!) - 它正在工作,但今天我正在尝试做简单的小部件......你能看到并帮助我吗?这适用于ADK和我的手机,但是: 应用程序Hello Widget(进程com.example.widget)意外停止。强行关闭。

我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.widget"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme"
    android:name=".zachodnik"
    android:label="@string/app_name">
    <receiver android:name=".zachodnik" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/zachodnik_widget_provider" />
    </receiver>
</application>

</manifest>

我的班级“zachodnik.java”

package com.example.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import static java.lang.Math.*;



public class zachodnik extends AppWidgetProvider {
private AppWidgetManager appWidgetManager;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Timer timer = new Timer();          
        timer.scheduleAtFixedRate(new ZachodyWschody(context, appWidgetManager),100, 10000);

    }

    private class ZachodyWschody extends TimerTask{

        RemoteViews remoteViews;
        ComponentName thisWidget;
        AppWidgetManager appWidgetManager;            
        private double Wsch, Zach;

        public ZachodyWschody(Context context,AppWidgetManager appWidgetManager){
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
            thisWidget = new ComponentName(context, zachodnik.class);                
        }

        public void algorytm(){

            Date data = new Date();

            int R =2009;// data.getYear();
            int M = 10; //data.getMonth();
            int D = 21;//data.getDay();

            double Long = 19; //dane dla Krakowa
            double Lat = 50;
            double Req = -0.833;
            double PI = 3.1415;

            double J = 367*R-(int)(7*(R+(int)((M+9)/12))/4)+(int)(275*M/9)+D-730531.5;
            double Cent = J/36525;
            double L = (4.8949504201433+628.331969753199*Cent) % 6.28318530718; //modulo
            double G = (6.2400408+628.3019501*Cent) % 6.28318530718;
            double O = 0.409093-0.0002269*Cent;
            double F = 0.033423 * sin(G) + 0.00034907 * sin(2*G);
            double E = 0.0430398 * sin(2*(L+F)) - 0.00092502 * sin(4*(L+F)) - F;
            double A = asin(sin(O) * sin(L+F));
            double C = (sin(0.017453293*Req)-sin(0.017453293*Lat)*sin(A)) / (cos(0.017453293*Lat)*cos(A));

            Wsch = (PI - (E+0.017453293*Long+1*acos(C)))*57.29577951/15;
            Zach = (PI - (E+0.017453293*Long+(-1)*acos(C)))*57.29577951/15;


        }

        @Override
        public void run(){
            algorytm();
            int godzWsch =  (int)Wsch;               
            int godzZach = (int)Zach;

            double minWsch = 60*(Wsch-godzWsch);
            double minZach = 60*(Zach-godzZach);

            godzWsch = godzWsch + 2; //przesuniec dla naszej strefy czasowej
            godzZach = godzZach + 2;


            String wsch = String.valueOf(godzWsch+":"+(int)minWsch);
            CharSequence w = (CharSequence)wsch;
            String zach = String.valueOf(godzZach+":"+(int)minZach);
            CharSequence z = (CharSequence)zach;

            remoteViews.setTextViewText(R.id.widget_textview, "Wschód: " + w + "\nZachód:" +  z );
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);

        }
    }
}

“zachodnik_widget_provider.xml”

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

“的strings.xml”

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="action_settings">Settings</string>
    <string name="widget_text">Hello Widget!</string>
    <string name="app_name">Widget Zachodnik</string>
</resources>

此小部件应提供有关太阳升起和太阳落山的信息。

这里是errors from LogCat(imageshack JPG)

提前感谢您的回复。

1 个答案:

答案 0 :(得分:0)

乍一看,您似乎已将zachodnik类定义为AndroidManifest.xml中的应用程序。

这是为扩展android.app.Application的课程保留的 当您启动应用程序时,应用程序加载器无法实例化您的类,因为它需要Application对象而不是AppWidgetProvider,因此您将获得ClassCastException。

AndroidManifest.xml中删除application标记中的android:name =“。zachodnik”行

另一件事,请注意Java中的命名约定。例如,类名应以大写字母开头。