0

ランチャーに自動的に追加されるウィジェットがありますが、これらのウィジェットの一部は構成する必要があるため、onClick リスナーを設定して ID を取得し、ウィジェット情報を取得してアクティビティを起動し、ウィジェットを構成しますが、私が抱えている問題たとえば、天気ウィジェットのアクティビティから戻ったときです。失敗している部分は create メソッドにあります。何がうまくいかないのかわからない 誰かこの問題で私を助けてくれませんか??

    AppWidgetManager mAppWidgetManager;
    AppWidgetHost mAppWidgetHost;
    LinearLayout mainlayout;
    ComponentName cn = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainlayout = (LinearLayout) findViewById(R.id.mylauncher);
    mainlayout.setOnClickListener(this);

    mAppWidgetManager = AppWidgetManager.getInstance(this);
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);

    // search widgets
    searchWidgets();

}





public void searchWidgets() {
    String widget = "com.weather.Weather.widgets.WeatherWidgetProvider4x1";
    try {

        mAppWidgetManager = AppWidgetManager.getInstance(getBaseContext());
        final List<AppWidgetProviderInfo> infos = mAppWidgetManager
                .getInstalledProviders();
        AppWidgetProviderInfo appWidgetInfo = null;
        for (final AppWidgetProviderInfo info : infos) {
            Log.v("Available Widgets", "label " + info.label + " ::"
                    + info.provider.getPackageName() + " / "
                    + info.provider.getClassName());
        }

        for (final AppWidgetProviderInfo info : infos) {
            Log.v("Searching for Widget class",
                    info.provider.getPackageName() + " / "
                            + info.provider.getClassName());

            if (widget.equals(info.provider.getClassName())) {
                String packageName = info.provider.getPackageName()
                        .toString();
                cn = new ComponentName(packageName, widget);
                // found it
                appWidgetInfo = info;
                Log.d("FOUND !!!!!!!!!", "Widget class " + packageName);

                break;
            }

        }

        mAppWidgetHost = new AppWidgetHost(getBaseContext(),
                R.id.APPWIDGET_HOST_ID);

        if (appWidgetInfo == null) {
            Log.d(TAG, cn + " No such widget available returning view");

            // return; // stop here
        } else {

            int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

            AppWidgetHostView hostView = mAppWidgetHost.createView(this,
                    appWidgetId, appWidgetInfo);
            hostView.setAppWidget(appWidgetId, appWidgetInfo);

            AppWidgetHostView hostViewsecond = mAppWidgetHost.createView(
                    this, appWidgetId, appWidgetInfo);
            hostView.setAppWidget(appWidgetId, appWidgetInfo);

            hostView.setId(appWidgetInfo.previewImage);

            Drawable draw = null;
            String packageName = appWidgetInfo.provider.getPackageName();
            try {
                draw = this.getPackageManager().getDrawable(packageName,
                        appWidgetInfo.previewImage,
                        this.getPackageManager().getApplicationInfo(packageName,
                                0));
            } catch (NameNotFoundException e) {
                Log.d(TAG, "drawable not found",e);
            }

            ImageView preViewWigdet = new ImageView(this);
            preViewWigdet.setImageDrawable(draw);
            preViewWigdet.setId(appWidgetInfo.previewImage);


            mainlayout.addView(preViewWigdet);

        }

    } catch (SecurityException securityEx) {
        Log.e("Bind Widget",
                "Error device not rooted,  returning view, Security exception:- ",
                securityEx);

    }
}



@Override
public void onClick(View v) {
    AppWidgetProviderInfo widgetItem = null;
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

    if (view.getClass().equals(ImageView.class)){
    // putting installed Providers in a list
    final List<AppWidgetProviderInfo> infos = mAppWidgetManager
            .getInstalledProviders();
    for (int i = 0; i < infos.size(); i++) {
        if (infos.get(i).previewImage == view.getId()) {
            widgetItem = (AppWidgetProviderInfo) infos.get(i);
        }
    }
    ComponentName cn = new ComponentName(
            widgetItem.provider.getPackageName(),
            widgetItem.provider.getClassName());

    int[] ids;
    ids = mAppWidgetManager.getAppWidgetIds(cn);
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);

    intent.setComponent(widgetItem.configure);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

    if (widgetItem.configure != null) {
        Log.d(TAG, "widget to be configured " + widgetItem.label);
        ((Activity) this).startActivityForResult(intent,
                R.id.REQUEST_CREATE_APPWIDGET);
    }

}
}



/**
 * If the user has selected an widget, the result will be in the 'data' when
 * this function is called.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "" + resultCode);
    if (resultCode == RESULT_OK) {
        if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
            configureWidget(data);
        }
        if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
            mAppWidgetManager = AppWidgetManager.getInstance(this);
            createWidget(data);
        }
    } else if (resultCode == RESULT_CANCELED && data != null) {
        int appWidgetId = data.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        if (appWidgetId != -1) {
            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

/**
 * Checks if the widget needs any configuration. If it needs, launches the
 * configuration activity.
 */
private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras
            .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager
            .getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = new Intent(
                AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
    } else {
        createWidget(data);
    }
}

/**
 * Creates the widget and adds to our view layout.
 */
public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras
            .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);

  //this is where I am having the problem, appWidgetInfo returns null
    AppWidgetProviderInfo appWidgetInfo = this.mAppWidgetManager
            .getAppWidgetInfo(appWidgetId);

    AppWidgetHostView hostView = mAppWidgetHost.createView(this,
            appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    mainlayout.addView(hostView);

    Log.i(TAG, "The widget size is: " + appWidgetInfo.minWidth + "*"
            + appWidgetInfo.minHeight);
}
4

1 に答える 1

0

「this.mAppWidgetManager.getAppWidgetInfo(appWidgetId)」という行がnullを返すと思います.Appwidget IDをAppwidgethostInfoプロバイダーにバインドするコードのどこにも表示されない

bindAppWidgetIdIfAllowed 関数をグーグル検索して、その仕組みを確認してください。ACTION_APPWIDGET_BIND に対して別のアクティビティ呼び出しを行う必要がある場合があります。

于 2014-02-13T22:49:16.557 に答える