0

该类是另一个类的扩展,当我弄乱列表视图时遇到错误。谁能帮我?布局:网格布局

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

4

3 回答 3

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); 
}

}

于 2013-11-11T03:57:57.610 回答
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); 

希望这可以帮助。

于 2013-11-11T04:01:13.010 回答
0
// try this
View  view = LayoutInflater.from(MyActivity.this).inflate(R.layout.XML,null,false);
lvContext = (ListView) view.findViewById(R.id.lvContexts);
于 2013-11-11T04:04:22.557 回答