我有一个问题,模拟器显示警告对话框“不幸的是,AppName已停止”感谢您的帮助。我想创建时钟小部件。
RmWidget.java
包lek.runningman.widget;
import java.text.SimpleDateFormat; import java.util.Date; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.widget.RemoteViews; public class RmWidget extends AppWidgetProvider { // Define the format string for the date private SimpleDateFormat formatter = new SimpleDateFormat( "hh:mm:ss\nEEEEEEEE\nd-MMM-yyyy"); @Override public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Retrieve and format the current date String now = formatter.format(new Date()); // Change the text in the widget RemoteViews updateViews = new RemoteViews( context.getPackageName(), R.layout.main); updateViews.setTextViewText(R.id.text, now); appWidgetManager.updateAppWidget(appWidgetIds, updateViews); //super.onUpdate(context, appWidgetManager, appWidgetIds); onUpdate(context, appWidgetManager, appWidgetIds); } }
widget.xml
android:minWidth="220dip" android:minHeight="146dip" android:updatePeriodMillis="1000" android:initialLayout="@layout/main"> </appwidget-provider>
main.xml中
<TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="@string/app_name" android:textStyle="bold" android:typeface="monospace" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout>
RmWidget Manefest.xml
<uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <receiver android:name=".RmWidget" android:label="@string/widget_name" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /> </receiver> </application> </manifest>
答案 0 :(得分:1)
onUpdate的无限递归,在最后一行调用onUpdate。只需删除电话。
答案 1 :(得分:-1)
创建以下方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_filename);
}
初始化布局非常重要,否则会产生一些问题。