2013-01-15 35 views
-2

嗨我想做一個android小部件。在這個小部件中,我試圖通過調用Web服務並從中獲取數據並在特定的時間間隔後在文本視圖中顯示它並更新我的文本視圖的值,並且出現異常。獲取例外:在Android小部件

Exception:java.lang.RuntimeException: Unable to start service [email protected]{cmp=com.example.newwidget 
/.UpdateService (has extras) }: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

這裏是我試圖代碼,

Widget.java

import android.appwidget.AppWidgetManager; 
    import android.appwidget.AppWidgetProvider; 
    import android.content.ComponentName; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.widget.RemoteViews; 
    import android.widget.RemoteViews.RemoteView; 
    import android.widget.Toast; 

    public class Widget extends AppWidgetProvider { 


@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 
    Toast.makeText(context, "OnUpdate", Toast.LENGTH_LONG).show(); 

     ComponentName thisWidget = new ComponentName(context, 
       Widget.class); 
      int[] allWidgetIds =  appWidgetManager.getAppWidgetIds(thisWidget); 
      Toast.makeText(context, "allWidgetIds", Toast.LENGTH_LONG).show(); 

      // Build the intent to call the service 
      Intent intent = new Intent(context.getApplicationContext(), 
       UpdateService.class); 
      Toast.makeText(context, "call UpdateService", Toast.LENGTH_LONG).show(); 

      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 
      Toast.makeText(context, "", Toast.LENGTH_LONG).show(); 

      // Update the widgets via the service 
      context.startService(intent); 
} 

} 

UpdateService.java

import java.io.IOException; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.client.ClientProtocolException; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.ResponseHandler; 
    import org.apache.http.client.methods.HttpGet; 
    import org.apache.http.impl.client.BasicResponseHandler; 
    import org.apache.http.impl.client.DefaultHttpClient; 

    import android.app.ProgressDialog; 
    import android.app.Service; 
    import android.appwidget.AppWidgetManager; 
    import android.content.ComponentName; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.IBinder; 
    import android.util.Log; 
    import android.widget.RemoteViews; 
    import android.widget.Toast; 

    public class UpdateWidget extends Service { 
    private RemoteViews views; 
    private String url; 
    private String strAPIRender; 
    private final HttpClient Client = new DefaultHttpClient(); 
    private String Content; 
    private String Error = null; 
    private ProgressDialog Dialog = new ProgressDialog(null); 
    private HttpResponse response; 
    private HttpEntity httpEntity; 


@Override 
    public void onStart(Intent intent, int startId) { 
     Log.d("AppWidget.UpdateService", "onStart()"); 
    Toast.makeText(UpdateWidget.this, "Onstart", Toast.LENGTH_LONG).show(); 
     // Build the widget update for today 
    Toast.makeText(UpdateWidget.this, "Updateviews", Toast.LENGTH_LONG).show(); 

     RemoteViews updateViews = buildUpdate(this); 
     Log.d("WordWidget.UpdateService", "update built"); 

Toast.makeText(UpdateWidget.this, "buildupdate finish", Toast.LENGTH_LONG).show(); 

     // Push update for this widget to the home screen 
    ComponentName thisWidget = new ComponentName(this, WidgetAppActivity.class); 

     AppWidgetManager manager = AppWidgetManager.getInstance(this); 

    Toast.makeText(UpdateWidget.this, "final update", Toast.LENGTH_LONG).show(); 
    manager.updateAppWidget(thisWidget, updateViews); 

    Log.d("WordWidget.UpdateService", "widget updated"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    public RemoteViews buildUpdate(Context context) { 
     // Pick out month names from resources 
Toast.makeText(UpdateWidget.this, "buildupdate", Toast.LENGTH_LONG).show(); 
url = "http://www.webservicex.net/currencyconvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=INR"; 

     grabURL(url); 
Toast.makeText(UpdateWidget.this, "grabURL finish", Toast.LENGTH_LONG).show(); 

     String result = strAPIRender; 

views = new RemoteViews(context.getPackageName(), R.layout.activity_widget_app); 
     views.setTextViewText(R.id.update, result); 
     return views; 
    } 
    public void grabURL(String url) { 
Toast.makeText(UpdateWidget.this, "in grabURL method", Toast.LENGTH_LONG).show(); 



     Toast.makeText(null, "execute url", Toast.LENGTH_LONG).show(); 
     try { 
      HttpGet httpget = new HttpGet(url); 
    ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      Content = Client.execute(httpget, responseHandler); 
     } catch (ClientProtocolException e) { 
      Error = e.getMessage(); 
     //cancel(true); 
    } catch (IOException e) { 
      Error = e.getMessage(); 
      //cancel(true); 
     } 



     parseXml(Content); 
    } 



    public String parseXml(String content) { 
     try { 

      System.out.println(content); 
      try { 

       strAPIRender = XMLHandler.GetTagValue("double",content); 


      } catch (Exception e) { 
       System.out.println(" catch b"); 
      } 

     } catch (Exception e) { 
      System.out.println(" exception in parseXML"); 
     } 
     return strAPIRender; 


    } 


} 

清單文件

 <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <receiver 
     android:name=".Widget" 
     android:icon="@drawable/ic_launcher" 
     android:label="A Widget" > 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/widget_info" /> 
    </receiver> 
    <service android:name="com.example.newwidget.UpdateService"></service> 
    </application> 

回答

0

從UpdateService刪除以下行:

private ProgressDialog Dialog = new ProgressDialog(null); 

這種「對話」沒有出現(在這個類的一瞥)在任何地方使用。如果你打算使用它,你不能像這樣實例化它(它需要一個真正的上下文)。這應該可以解決你所問的具體錯誤。 (即使你修正了這個錯誤,我也不確定這段代碼是否能按預期工作),你可能還想考慮一下,僅僅爲了初學者:爲調試目的去掉所有的吐司,並且使用logging和logcat,並且/或一個調試器;不要使用主UI線程(默認情況下也使用這些服務)進行網絡I/O(請參閱IntentService以獲得簡單的解決方法);不要使用來自服務的對話框/警報/ toast;以及注意非標準命名約定,如大寫起始成員變量,這會使代碼難以遵循(特別是當它不一致時,即使它不遵循約定)。)

+0

感謝您的幫助。 .i會嘗試改進我的代碼。 –