问题描述
Eclipse正在标记findViewById(int)所不确定的;它在做同样的事情getResources(),但我可以绕过通过调用context.getResources(),而不是(如下图所示),并似乎无法找到一个类似的解决方法findViewById。这里是code:
Eclipse is marking findViewById(int) as undefined; it was doing the same thing for getResources(), but I was able to get around that by calling context.getResources() instead (as seen below) and can't seem to 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);
}
难道还有其他的包我需要导入的findViewById?先谢谢了。
Is there another package I need to import for findViewById? Thanks in advance.
推荐答案
在code的工作而有所不同,你正在使用AppWidgets时。从本质上讲,你需要与远程视窗,而不是传统的按钮和findViewByIds工作。看到这个similar问题,获取有关如何写AppWidgets链接。特别是,两条链路用于developer.com教程。
The code works rather differently when you're using AppWidgets. Essentially you need to be working with RemoteView rather than traditional Buttons and findViewByIds. See this similar question for links on how to write AppWidgets. In particular, the two links for developer.com tutorials.
这篇关于findViewById未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!