-2

こんにちは、Android ウィジェットを作成しようとしています。このウィジェットでは、Web サービスを呼び出してテキスト ビューの値を更新し、そこからデータを取得して、特定のタイ間隔の後にテキスト ビューに表示しようとしていますが、例外が発生しています。

Exception:java.lang.RuntimeException: Unable to start service com.example.newwidget.UpdateService@40ce9518withIntent{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>
4

1 に答える 1

0

UpdateService から次の行を削除します。

private ProgressDialog Dialog = new ProgressDialog(null);

その「ダイアログ」は(そのクラスを一目見ただけで)どこでも使用できるようには見えません。それを使用するつもりなら、そのようにインスタンス化することはできません (実際のコンテキストが必要です)。これにより、質問している特定のエラーが解決されるはずです。

(そのエラーを修正しても、このコードが期待どおりに機能するかどうかはわかりません。初心者向けに、デバッグ目的ですべてのトーストを削除し、ロギングとログキャット、および/またはデバッガーを使用することも検討してください。ネットワーク I/O にメイン UI スレッド (サービスもデフォルトで使用) を使用しない (これを回避する簡単な方法については、IntentService を参照してください); サービスからのダイアログ/アラート/トーストを実行しないでください。 -メンバー変数を大文字で開始するなどの標準的な命名規則により、コードに従うのが難しくなります (特に、規則に従っていなくても、一貫性がない場合)。

于 2013-01-15T16:41:54.443 に答える