2

当我将屏幕旋转到横向并且当我想从我的自定义列表视图中选择一个对象时是一个例外,但是当屏幕处于纵向时一切正常。

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


主要活动:

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

天气适配器:

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

1 回答 1

0

我的猜测是,在旋转屏幕后重新创建活动(通常会发生)并单击 ListView 时,您尝试访问 Activity 中不再有效的对象。

于 2013-04-20T20:22:39.027 回答