findViewById中的Error ListView

时间:2013-11-11 03:32:41

标签: android listview spinner findby

该类是另一个类的扩展,当我搞乱listview时遇到错误。谁能帮我? 布局:GridLayout

XML:

<ListView
    android:id="@+id/lvContexts"
    android:layout_width="899dp"
    android:layout_height="356dp"
    android:layout_column="1"
    android:layout_columnSpan="3"
    android:layout_gravity="left"
    android:layout_row="3"
    android:layout_rowSpan="2" >
</ListView>

类别:

public class EditarContext extends Iniciar {
    ....

    lvContext = (ListView) findViewById(R.id.lvContexts);
ArrayList<String> lvList = (ArrayList<String>) contexts;
ArrayAdapter<String> spArrayAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, lvList);
lvContext.setAdapter(spArrayAdapter);
lvContext.setClickable(true);
lvContext.setVisibility(View.VISIBLE);
lvContext.setVisibility(View.GONE);
}

OBS:ListView的内容正在被选中内容微调器。

错误就是这些,但我不会错过任何错误:http://i.stack.imgur.com/Gdsdt.png

3 个答案:

答案 0 :(得分:0)

试试这个:

public class YourActivity extends Activity 
{
     private ListView lvContext;

     public void onCreate(Bundle saveInstanceState) {
        setContentView(R.layout.your_layout);

        lvContext = (ListView) findViewById(R.id.your_list_view_id);
        // Instanciating an array list
        ArrayList<String> lvList = new ArrayList<String>();
        lvList.add("foo");
        lvList.add("bar");

        // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
        ArrayAdapter<String> spArrayAdapter =      
        new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lvList);

        lvContext.setAdapter(spArrayAdapter); 
}

}

答案 1 :(得分:0)

要使用findViewByID,您应该扩展活动。如果没有,则需要通过将其作为参数传递给构造函数来实例化您的活动。

EditarContext instance = new EditarContext(this);

..

public class EditarContext extends Iniciar {

public Activity activity; 
//.... other attributes 

public EditarContext( Activity _activity){

   this.activity = _activity;
//other initializations...

}
}

然后你可以像这样使用findViewById:

lvContext = (ListView)this.activity.findViewById(R.id.lvContexts); 

希望这有帮助。

答案 2 :(得分:0)

// try this
View  view = LayoutInflater.from(MyActivity.this).inflate(R.layout.XML,null,false);
lvContext = (ListView) view.findViewById(R.id.lvContexts);