0

昨天我开始在 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"
/>

“字符串.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>

此小部件应提供有关日出和日落的信息。

这是来自 LogCat 的错误(imageshack JPG)

预先感谢您的回复。

4

1 回答 1

0

乍一看,您似乎已将您的zachodnik类定义为AndroidManifest.xml.

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

在您从标签AndroidManifest.xml中删除 android:name=".zachodnik" 行application

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

于 2013-04-09T16:00:24.553 回答