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 をクリックすると、無効になったアクティビティ内のオブジェクトにアクセスしようとします。

于 2013-04-20T20:22:39.027 に答える