FindViewById undefined

Eclipse marks findViewById (int) as undefined; it did the same for getResources (), but I managed to get around this by calling context.getResources () instead (as shown below) and cannot find a similar workaround for findViewById. Here is the code:

package com.myapp.android.MyWidget; import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.ComponentName; import android.content.pm.PackageManager; import android.content.res.Resources; import android.util.Log; import android.view.View; import android.widget.RemoteViews; import android.widget.Button; import android.os.Bundle; public class MyWidget extends AppWidgetProvider { private static String[] states; @Override public void onEnabled(Context context) { final Button button = (Button) findViewById(R.id.widget_state_button); states = context.getResources().getStringArray(R.array.states); } 

Is there any other package I need to import for findViewById? Thanks in advance.

+4
source share
2 answers

The code works differently when you use AppWidgets. Essentially, you need to work with RemoteView, not with traditional buttons and findViewByIds. See this similar question for links on how to write AppWidgets. In particular, two links for developer.com tutorials.

+2
source

You are expanding the AppWidgetProvider . But the findViewById method is not implemented there; findViewById defined in Activity .

+1
source

Source: https://habr.com/ru/post/1310720/


All Articles