【问题标题】:findViewById is undefined for type <my class name is here> in AppWidgetProvider在 AppWidgetProvider 中未定义类型 <my class name is here> 的 findViewById
【发布时间】:2011-10-28 02:36:55
【问题描述】:

是否有可以用于 findViewById 的等效代码行? Eclipse 告诉我它在 AppWidgetProvider 中的类型未定义。

我正在尝试为按钮设置点击处理程序。

public class WidgetProvider extends AppWidgetProvider implements OnClickListener{

    final Button buttonOnOff = (Button) findViewById(R.id.button_on_off);

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

        /*
         * Set Click Listener
         */
        buttonOnOff.setOnClickListener(this);

        /*
         * The following 3 lines of code are required to display the widget on the screen.
         */
        RemoteViews remoteView = new RemoteViews(context.getPackageName(),
            R.layout.appwidget_layout);

        ComponentName myWidget = new ComponentName(context,
            WidgetProvider.class);
        appWidgetManager.updateAppWidget(myWidget, remoteView);

    }

    @Override
    public void onClick(View v) {
    }
}

【问题讨论】:

    标签: android onclick listener undefined android-appwidget


    【解决方案1】:

    当您使用 AppWidgets 时,代码的工作方式大不相同。本质上,您需要使用 RemoteView 而不是传统的 Button 和 findViewByIds。有关如何编写 AppWidgets 的链接,请参阅此 related answer。特别是 developer.com 教程的两个链接。

    【讨论】:

    • 您好,感谢您的链接。真的,埃马德
    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多