2011-10-27 63 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

感謝您的代碼示例。 :-) –