tags:

views:

57

answers:

2

Hello, I want to start an Activity, when clicking on Button (Buttons are generated in GridView). I have tried everything, but it fails when clicking the button in emulator... :(

Here is the code of class implementing OnClickListener

public class CalendarMonthClickListener extends Activity implements OnClickListener {

private final Context mContext;
private final int mPosition;
private final int mDay;
private final int mMonth;
private final int mYear;
private final int mPositionStart;
private final int mNumberOfDays;

public CalendarMonthClickListener(Context mContext, int position, 
        int month, int year, int positionStart, int numberOfDays) {

    this.mContext = mContext;
    this.mPosition = position;
    this.mMonth = month;
    this.mYear = year;
    this.mPositionStart = positionStart;
    this.mNumberOfDays = numberOfDays;
    this.mDay = position - positionStart - 6;
}

@Override
public void onClick(View view) {
    try {
        Intent intent = new Intent(this.mContext, CalendarDetail.class);
        startActivity(intent); // here I get NullPointerException 
    } catch (Exception e) {
        Log.e("CalendarMonthClickListener", e.getMessage(), e);
    }
}

There is a NullPointerException on the line where I start Activity - startActivity(intent);

Class CalendarMonth (default class, holds the layout):

public class CalendarMonth extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar_month);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setNumColumns(7);
    gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    gridview.setBackgroundColor(Color.BLACK);
    gridview.setAdapter(new CalendarMonthAdapter(this));
}

}

Class CalendarMonthAdapter (creates gridview form buttons):

public class CalendarMonthAdapter extends BaseAdapter {

private Context mContext;
private String mDaysIds[] = { 
        "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su", 
        "", "", "", "",
        "1", "2", "3", "4", "5", "6", "7", 
        "8", "9", "10", "11", "12", "13", "14", 
        "15", "16", "17", "18", "19", "20", "21", 
        "22", "23", "24", "25", "26", "27", "28", 
        "29", "30", "31" };

private Calendar calendar;

private int mCurrentDay;
private int mCurrentMonth;
private int mCurrentYear;
private int mNumOfDays;
private int mPositionStart;

/**
 * Konstruktor
 * 
 * @param context
 */
public CalendarMonthAdapter(Context context) {

    mContext = context;

    calendar = Calendar.getInstance();

    getCurrentDay();

    this.calendar.set(mCurrentYear, mCurrentMonth - 1, 1);

    this.mNumOfDays = this.calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int start = this.calendar.get(Calendar.DAY_OF_WEEK);
    if (start == 1) {
        this.mPositionStart = 6;
    } else {
        this.mPositionStart = start - 2;
    }
}

@Override
public int getCount() {
    return mDaysIds.length;
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Button button;

    if (convertView == null) {

        button = new Button(mContext);
        button.setLayoutParams(new GridView.LayoutParams(60, 60));
        button.setPadding(5, 5, 5, 5);

    } else {
        button = (Button) convertView;
    }

    button.setText(this.mDaysIds[position]);
    button.setId(position);
            // here is the Listener
    button.setOnClickListener(new CalendarMonthClickListener(
        this.mContext, position, this.mCurrentMonth, this.mCurrentYear, this.mPositionStart, this.mNumOfDays));
    }
    return button;
}

}

Here is the LOG

Thanks for help in advance

A: 

this.mContext is a not a instance of the Activity. you have to start a activity from a activity. so try this.

Intent intent = new Intent(CalendarMonthClickListener.this, CalendarDetail.class);
Praveen Chandrasekaran
Didn't work for me :( ...I've uploaded whole log (see above)
Peter O.
@Peter: where did you setOnClickListener for your button in your code???
Praveen Chandrasekaran
please look at the codes above...There are 3 classes I use
Peter O.
A: 

Try obtaining the context via getApplicationContext()

Intent intent = new Intent(getApplicationContext(), CalendarDetail.class);
Anuj Tenani
not working :( ...log: 10-29 08:55:06.528: ERROR/CalendarMonthClickListener(290): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
Peter O.