2013-04-20 57 views
2

当我旋转屏幕到风景,当我想从我的自定义列表视图中选择一个对象是一个例外,但是当屏幕是肖像一切都很好。listview onclick后旋转到风景

旋转到横向并单击列表视图项目活动后会引发异常。


MainActivity:

WeatherAdapter adapter; 
      ListView view; 
      Button btn; 
      /* Row item */ 
      View h; 
      Setting set; 

     TextView u,kz; 
     RelativeLayout lay; 

     Weather selected; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.d("onCreate()",(" " + set.last).toString()); 
     btn = (Button) findViewById(R.id.gonext); 
     btn.setOnClickListener(this); 

     kz = (TextView) findViewById(R.id.textView1); 


     view = new ListView(this);  

     adapter = new WeatherAdapter(this, 
       R.layout.list_view_item_row, set.weather_data); 

     view = (ListView) findViewById(R.id.listView1); 
     /* 
     View header = (View)getLayoutInflater().inflate(R.layout.list_view_header_row, null); 
     view.addHeaderView(header); 
     */ 
     view.setAdapter(adapter); 

    view.setOnItemClickListener(this); 
    //view.setOnItemSelectedListener(this); 
     if(set.last != -1){ 
      View cc = view.getChildAt(set.last); 
      Log.d("Choosed",("last = " + set.last).toString()); 
      RelativeLayout r = (RelativeLayout) cc.findViewById(R.id.ItemLayout); 
      r.setBackgroundColor(Color.BLUE); 
      r.refreshDrawableState(); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(v.getId() == R.id.gonext){ 

      selected = new Weather(set.weather_data[set.last]); 

      Intent intent = new Intent(this,Choosed_Activity.class); 
      intent.putExtra("selected_i", selected.icon); 
      intent.putExtra("selected_t", selected.title); 
      this.startActivity(intent); 




     } 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     //h = (View) view.getChildAt((int) arg3); 
     if(set.last != -1){ 
      h = (View) (view.getChildAt(set.last)); 
      u = (TextView) h.findViewById(R.id.txtTitle); 
      lay = (RelativeLayout) h.findViewById(R.id.ItemLayout); 
      lay.setBackgroundColor(Color.GRAY); 
      u.setText("FAFAFAF"); 
      } 
     //} 
     h = (View) (view.getChildAt((int) arg3)); 
     u = (TextView) h.findViewById(R.id.txtTitle); 
     lay = (RelativeLayout) h.findViewById(R.id.ItemLayout); 
     lay.setBackgroundColor(Color.BLUE); 
     u.setText(("siam " + arg2).toString()); 

     set.last = (int) arg3; 

     if(set.last > -1) 
      btn.setEnabled(true); 
    } 

WeatherAdapter:

public class WeatherAdapter extends ArrayAdapter<Weather>{ 



    Context context; 
    int layoutResourceId; 
    Weather data[] = null; 

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
     Log.d("WEATHER ADAPETR","Constructor()"); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     WeatherHolder holder = null; 

     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new WeatherHolder(); 
      holder.imgIcon = (ImageView)row.findViewById(R.id.imageView1); 
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 
      holder.rL = (RelativeLayout) row.findViewById(R.id.ItemLayout); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (WeatherHolder)row.getTag(); 
     } 

     Weather weather = data[position]; 
     holder.txtTitle.setText(weather.title); 
     holder.imgIcon.setImageResource(weather.icon); 
     holder.rL.setBackgroundColor(Color.GRAY); 

     return row; 
    } 

    static class WeatherHolder 
    { 
     ImageView imgIcon; 
     TextView txtTitle; 
     RelativeLayout rL; 
    } 
} 

天气:

public class Weather { 
    public int icon; 
    public String title; 

    public Weather(){ 
     super(); 
    } 
    public Weather(Weather w){ 
     this.icon = w.icon; 
     this.title = w.title; 
    } 
    public Weather(int icon, String title) { 
     super(); 
     this.icon = icon; 
     this.title = title; 
    } 
} 
+1

请提供堆栈跟踪+一些代码,您是否还使用自定义适配器? – Reda 2013-04-20 20:21:34

回答

0

我的猜测是,旋转活动重新在屏幕上(如通常发生后, )并点击L istView尝试访问不再有效的活动内的对象。

+0

这是代码:http://wklej.org/id/1017876/ – 2013-04-20 20:52:37

+0

你能告诉我们什么异常吗? logcat将不胜感激。 – 2013-07-10 05:10:49