findViewById未定义 [英] findViewById undefined

查看:335
本文介绍了findViewById未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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工作。看到这个<一href="http://stackoverflow.com/questions/2858648/android-home-screen-widget-documentation/2864376#2864376">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未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆