2011-10-27 89 views
0

我做的第一次一个简单的小工具,它似乎我使用的教程有缺少的东西,因为我的logcat的状态“错误充气AppWidget”,从弹出的窗口小部件列表中选择它的时候。错误充气AppWidget

按照教程我做这些。

布局:

<TextView android:id="@+id/widget_textview" android:text="@string/widget_text" 
    android:layout_height="wrap_content" android:layout_width="wrap_content" 
    android:layout_gravity="center_horizontal|center" 
    android:layout_marginTop="5dip" android:padding="10dip" 
    android:textColor="@android:color/black" /> 
</LinearLayout> 

类别:

package hello.widget; 

import android.appwidget.AppWidgetProvider; 

public class HelloWidget extends AppWidgetProvider { 
} 

字符串:

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

窗口小部件提供者:

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

清单:

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

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

    <!-- Broadcast Receiver that will process AppWidget updates --> 
    <receiver android:name="hello.widget.HelloWidget" 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/hello_widget_provider" /> 
    </receiver> 

    </application> 
</manifest> 

回答

1

你缺少你的布局文件开幕<LinearLayout>。你在这里什么都不应该编译。

您还缺少你AppWidgetProvider的整个实施。你需要实现onUpdate()来指定应用小部件应该显示的内容。

此外,您updatePeriodMillis短于允许的 - 你不能更新应用每隔10秒插件这种方式。

此外,请确保您的布局命名为main.xml,或更新您的android:initialLayout以反映布局的正确名称。

+0

您好,感谢您的帮助。它现在正在运行。 –

1

我的解决办法:

public void onReceive(Context context, Intent intent) 
{ 
    String action = intent.getAction(); 

    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) 
    { 
     long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400; 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 

     //Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock")); 
     // PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0); 
     Intent Date_Change= new Intent(Intent.ACTION_DATE_CHANGED); 
     PendingIntent pendingIntent2=PendingIntent.getActivity(context,0, Date_Change, 0); 

     views.setOnClickPendingIntent(R.id.textView1, pendingIntent2); 
     views.setTextViewText(R.id.textView1,""+days); 

     //views.setOnClickPendingIntent(R.id.AnalogClock, pendingIntent); 

     //AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views); 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
     ComponentName thisWidget = new ComponentName(context, Widget.class); 
     appWidgetManager.updateAppWidget(thisWidget, views); 
    } 
} 
+0

感谢您的代码示例。 :-) –