Listactivity具有多个按钮NullPointerException仅在横向模式下具有滚动列表

时间:2012-08-31 07:53:47

标签: android

我有一个listactivity应用程序,由5行组成,每行包含一个TEXT和5个BUTTON,文本和按钮都可以点击,文本将打开MyDay活动,它是textview,当你点击任何按钮时它必须打开videoview屏幕。

每个东西运行得很好除了当将移动设备转为横向模式并滚动列表时它强行关闭并在此行中的logcat报告中给出 NullPointerException  (51) MyArrayAdapter Class

logcat:

  java.lang.NullPointerException
         at com.test.demo.MyArrayAdapter.getView(MyArrayAdapter.java:51)

MyArrayAdapter类中的第(51)行是:

   holder.textView.setText(s);

任何建议都将受到赞赏,谢谢。

MyArrayAdapter类:

 public class MyArrayAdapter extends ArrayAdapter<String> {

private final String[] mClasses;
private Context mContext;
private Typeface tf;
private LayoutInflater mInflater;
private String[] mData = { "button1", "button2", "button3", "button4",
        "button5" };
private int[] ids = { R.id.button1, R.id.button2, R.id.button3,
        R.id.button4, R.id.button5 };

int[][] rowDrawables = { 
        {R.drawable.row_1, R.drawable.row_2, R.drawable.row_3, 
               R.drawable.row_4, R.drawable.row_5},
        {R.drawable.row_6, R.drawable.row_7, R.drawable.row_8, 
               R.drawable.row_9, R.drawable.row_10},
        {R.drawable.row_11, R.drawable.row_12, R.drawable.row_13, 
               R.drawable.row_14, R.drawable.row_15},
        {R.drawable.row_16, R.drawable.row_17, R.drawable.row_18, 
               R.drawable.row_19, R.drawable.row_20},
        {R.drawable.row_21, R.drawable.row_22, R.drawable.row_23, 
               R.drawable.row_24, R.drawable.row_25}};

public MyArrayAdapter(Context context, String[] classes) {
    super(context, 0, classes);
    mClasses = classes;
    mContext = context;
    tf = Typeface.createFromAsset(context.getAssets(), "BFantezy.ttf");
    mInflater = LayoutInflater.from(context);
                     }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row, null, true);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView
                .findViewById(R.id.row_label);
        holder.textView.setTypeface(tf);
        holder.bt1 = (Button) convertView.findViewById(R.id.button1);
        holder.bt2 = (Button) convertView.findViewById(R.id.button2);
        holder.bt3 = (Button) convertView.findViewById(R.id.button3);
        holder.bt4 = (Button) convertView.findViewById(R.id.button4);
        holder.bt5 = (Button) convertView.findViewById(R.id.button5);
    } else {
        holder = (ViewHolder) convertView.getTag();}

    String s = mClasses[position];
    holder.textView.setText(s);

    int[] rowDr = rowDrawables[position];
    holder.bt1.setBackgroundResource(rowDr[0]);
    holder.bt2.setBackgroundResource(rowDr[1]);
    holder.bt3.setBackgroundResource(rowDr[2]);
    holder.bt4.setBackgroundResource(rowDr[3]);
    holder.bt5.setBackgroundResource(rowDr[4]);

    holder.textView.setTag(Integer.valueOf(position));
    holder.textView.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Integer pos = (Integer) v.getTag();
            Intent ourIntent = new Intent(mContext, MyDay.class);
            ourIntent.putExtra("cheese", mClasses[pos]);
            mContext.startActivity(ourIntent);
        }
    });
    holder.bt1.setTag(Integer.valueOf(position));
    holder.bt1.setOnClickListener(mListener);
    holder.bt2.setTag(Integer.valueOf(position));
    holder.bt2.setOnClickListener(mListener);
    holder.bt3.setTag(Integer.valueOf(position));
    holder.bt3.setOnClickListener(mListener);
    holder.bt4.setTag(Integer.valueOf(position));
    holder.bt4.setOnClickListener(mListener);
    holder.bt5.setTag(Integer.valueOf(position));
    holder.bt5.setOnClickListener(mListener);
    return convertView;
                                 }

static class ViewHolder {
    TextView textView;
    Button bt1, bt2, bt3, bt4, bt5;

                        }

private OnClickListener mListener = new OnClickListener() {

    public void onClick(View v) {
                    Integer realPos = (Integer) v.getTag();
                    int video = 0;
        String night = null;
        for (int i = 0; i < ids.length; i++) {
            if (v.getId() == ids[i]) {
                video = i;
                break;
            }
        }
        Intent i = new Intent(mContext, My_videos.class);
        i.putExtra("video", video);
                    i.putExtra("row", realPos);
        mContext.startActivity(i);
                   }
             };

               }

ListButtons:

 public class ListButtons extends ListActivity {
String classes[] = {"First",  "Second", "Third","Fourth", "Fifth" }; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter(new MyArrayAdapter(this, classes));  }}

0 个答案:

没有答案