-2

在程序中,我试图将类的上下文传递给 INTENT,但View.OnClickListener()由于它包含在onClick()方法中而被传递。我应该写什么而不是this

package com.calender;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class GoTo extends Activity{

Spinner sp;
EditText etyear;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.go_to);
    sp = (Spinner) findViewById (R.id.spin);
    etyear = (EditText) findViewById (R.id.editText1);
    submit = (Button) findViewById (R.id.bsub);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent in = new Intent(this,NextCal.class);
            Bundle bun = new Bundle();
            bun.putString("month", sp.getSelectedItem().toString());
            int y = Integer.parseInt(etyear.getText().toString());
            bun.putInt("year", y);
            in.putExtras(bun);
            startActivity(in);
        }
    });


}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}


}
4

2 回答 2

1
Intent in = new Intent(GoTo.this,NextCal.class);

     will solve your problem.
于 2013-09-13T18:26:43.423 回答
1

您需要GoTo.this它为您提供活动的上下文,而不是 onClick 类

于 2013-09-13T18:26:49.203 回答