2013-09-30 133 views
-1

我想在我的應用程序中使用列表視圖,我沒有正確編碼,但是當我運行該應用程序時,它會關閉。這裏是我的代碼:我的android應用程序關閉

setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1,getResources().getStringArray(R.array.cities))); 

} 

適配器

private class MyAdapter extends ArrayAdapter<String>{ 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
      String[] string) { 
     super(context, resource, textViewResourceId, string); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     LayoutInflater in=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row=in.inflate(R.layout.list_item, parent, false); 
     String[] items=getResources().getStringArray(R.array.cities); 
     TextView tv=(TextView) row.findViewById(R.id.textView1); 

     return super.getView(position, convertView, parent); 



    } 

} 
} 
+0

崩潰日誌請 –

回答

0

更改爲以下

LayoutInflater in; 
String[] items; 
public MyAdapter(Context context, int resource, int textViewResourceId, 
      String[] string) { 
     super(context, resource, textViewResourceId, string); 
    in=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    items=context.getResources().getStringArray(R.array.cities); 

    } 

getResources()需要活動情境。

0

你可以嘗試這樣的事情:

public class MySimpleArrayAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

public MySimpleArrayAdapter(Context context, String[] values) { 
    super(context, R.layout.rowlayout, values); 
    this.context = context; 
    this.values = values; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label);  
    textView.setText(values[position]); 

    return rowView; 
} 
}