1

我得到一個arrayindexoutofbounds異常。我的適配器代碼是android arrayindexoutofbounds使用listview的異常

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class LevelAdapter extends ArrayAdapter<Level> { 

    static Context context; 
     static int layoutResourceId; 
     static Level data[] = null; 

    public LevelAdapter(Context context, int layoutResourceId, Level[] data) { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.context = context; 
      this.data = data; 
     } 

     /* public LevelAdapter(ShowFrag1 showFrag1, int listItem, 
       Level[] weather_data) { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.context = context; 
      this.data = data; 

      // TODO Auto-generated constructor stub 
     }*/ 

     public long getItemId(int position) { 
      return position; 
     } 

     @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.imgIcon); 
       holder.txtTitle = (TextView)row.findViewById(R.id.txt); 
       // holder.imgIcon2=(ImageView)row.findViewById(R.id.imgIcon2); 
       Typeface robotoLight = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf"); 
       holder.txtTitle.setTypeface(robotoLight); 

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

      Level weather = data[position]; 
      holder.txtTitle.setText(weather.title); 
     // holder.imgIcon.setImageResource(weather.icon); 

      return row; 
     } 

     static class WeatherHolder 
     { 
     // ImageView imgIcon; 
      TextView txtTitle; 
     // ImageView imgIcon2; 
     } 

} 

和使用ListView的片段中的代碼是這樣

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class AboutFrag3 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v=inflater.inflate(R.layout.aboutfrag3, container,false); 
     ListView lv1=(ListView) v.findViewById(R.id.listView1); 
     ListView lv2=(ListView)v.findViewById(R.id.listView2); 
     Level weather_data[] = new Level[] 
       { 

        new Level(R.drawable.sac1, "O63", R.drawable.play_button), 
        new Level(R.drawable.sn2, "O26",R.drawable.play_button), 
        new Level(R.drawable.sn3, "O",R.drawable.play_button), 
        new Level(R.drawable.sn4, "O83",R.drawable.play_button), 
        new Level(R.drawable.sn6, "O.23",R.drawable.play_button), 
        new Level(R.drawable.sn6, "O9",R.drawable.play_button), 
        new Level(R.drawable.sn6, "O96",R.drawable.play_button), 
        new Level(R.drawable.sn6, "T98",R.drawable.play_button), 
        new Level(R.drawable.sn6, "T37",R.drawable.play_button), 
        new Level(R.drawable.sn6, "T248",R.drawable.play_button), 
        new Level(R.drawable.sn6, "T.87",R.drawable.play_button), 
        new Level(R.drawable.s6, "Tee: 54.06",R.drawable.play_button), 
        new Level(R.drawable.sn6, "T1",R.drawable.play_button), 
        new Level(R.drawable.s6, "Te",R.drawable.play_button) 


       }; 
     Level weather_data2[] = new Level[] 
       { 
       new Level(R.drawable.s1,"O3",R.drawable.play_button), 
       new Level(R.drawable.s1,"O 154",R.drawable.play_button), 
       new Level(R.drawable.s1,"O2",R.drawable.play_button), 
       new Level(R.drawable.sh1,"O: 5.11",R.drawable.play_button), 
       new Level(R.drawable.s1, "Te8",R.drawable.play_button), 
       new Level(R.drawable.s1, "T5",R.drawable.play_button), 
       new Level(R.drawable.s1, "T-10",R.drawable.play_button), 
       new Level(R.drawable.s1, "T52",R.drawable.play_button) 
       }; 
     LevelAdapter adapter = new LevelAdapter(getActivity(), 
       R.layout.list_item, weather_data); 
     LevelAdapter adapter2 = new LevelAdapter(getActivity(), R.layout.list_item, weather_data2); 

     View header = inflater.inflate(R.layout.header2, null); 
     //View header2 = inflater.inflate(R.layout.header, null); 
     View header3 = inflater.inflate(R.layout.header3, null); 
     lv1.addHeaderView(header); 
     lv2.addHeaderView(header3); 
     lv1.setAdapter(adapter); 
     lv2.setAdapter(adapter2); 
     Utility.setListViewHeightBasedOnChildren(lv1); 
     Utility.setListViewHeightBasedOnChildren(lv2); 
     return v; 
    } 
} 

我收到的ArrayIndexOutOfBounds例外,在這條線

Level weather = data[position]; 

我要去哪裏錯了? 謝謝

回答

1

這是因爲你的data[]static。當你製作一個屬性static時,它使得從該類派生的所有對象的屬性都相同。要修復它,只需要使數據不是static

+0

哦,是的!感謝它的工作。 –

+0

沒問題,你也應該使你的'context'和'layoutResourceId'非靜態以防止將來出現問題。如果你不需要它們,不要添加修飾符。 – John

1

標記爲static的成員變量僅存在一次,無論您創建了多少個對象實例。兩個適配器共享相同的data成員。

相關問題