【问题标题】:Android findViewById cannot resolve variableAndroid findViewById 无法解析变量
【发布时间】:2015-09-07 16:44:32
【问题描述】:

我是 Android 开发的新手

我有这个 Activity 类

public class ElencoNotificheActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_elenco_notifiche);
    }
    private void setRefreshListener()
    {
        ListView elencoNotificheView = (ListView)findViewById(R.layout.elencoNotificheView);
    }
}

在我的活动课上出现以下错误:

错误:(18, 71) 错误: 找不到符号变量 elencoNotificheView

但在我的布局 XML 中,我有:

        <ListView
            android:id="@+id/elencoNotificheView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="true"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            />

【问题讨论】:

  • 你明白为什么 R.id 有效但 R.layout 无效吗?
  • 我没有深入调查......但我认为 R.id 包含视图的 ID,而 R.layout 布局

标签: java android android-layout android-listview


【解决方案1】:

应该是R.id.elencoNotificheView 而不是R.layout. elencoNotificheView

所以你的 ListView 应该是这样的:

ListView elencoNotificheView = (ListView)findViewById(R.id.elencoNotificheView);

【讨论】:

    【解决方案2】:

    你应该有R.id.elencoNotificheView:

    private void setRefreshListener()
    {
        ListView elencoNotificheView = (ListView)findViewById(R.id.elencoNotificheView);
    }
    

    【讨论】:

      【解决方案3】:

      应该是

      ListView elencoNotificheView = (ListView)findViewById(R.id.elencoNotificheView);
      

      而不是

      ListView elencoNotificheView = (ListView)findViewById(R.layout.elencoNotificheView);
      

      【讨论】:

        【解决方案4】:

        你需要在代码下面修复它..

         private void setRefreshListener()
            {
                ListView elencoNotificheView = (ListView)findViewById(R.layout.elencoNotificheView);
            }
        

        更新

        ListView elencoNotificheView = (ListView)findViewById(R.id.elencoNotificheView);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多