Android findViewById无法解析变量

时间:2015-09-07 16:44:33

标签: java android android-layout android-listview

我是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"
            />

4 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:2)

您应该R.id.elencoNotificheView

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

答案 2 :(得分:2)

应该是

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

而不是

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

答案 3 :(得分:1)

您需要在代码下面修复它。

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

更新

ListView elencoNotificheView = (ListView)findViewById(R.id.elencoNotificheView);
相关问题