HY!我需要为定义登录按钮的类实现startActivity。如何实现startActivity()的功能?
这是我的班级:
public class LoginButton extends ImageButton {
private Facebook mFb;
private Handler mHandler;
private SessionListener mSessionListener = new SessionListener();
private String[] mPermissions;
private Activity mActivity;
public LoginButton(Context context) {
super(context);
}
public LoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoginButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(final Activity activity, final Facebook fb) {
init(activity, fb, new String[] {});
}
public void init(final Activity activity, final Facebook fb,
final String[] permissions) {
mActivity = activity;
mFb = fb;
mPermissions = permissions;
mHandler = new Handler();
setBackgroundColor(Color.TRANSPARENT);
setAdjustViewBounds(true);
setImageResource(fb.isSessionValid() ?
R.drawable.logout_button :
R.drawable.login_button);
drawableStateChanged();
SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener(mSessionListener);
setOnClickListener(new ButtonOnClickListener());
}
private final class ButtonOnClickListener implements OnClickListener {
public void onClick(View arg0) {
if (mFb.isSessionValid()) {
SessionEvents.onLogoutBegin();
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFb);
asyncRunner.logout(getContext(), new LogoutRequestListener());
} else {
mFb.authorize(mActivity, mPermissions,
new LoginDialogListener());
Intent i = new Intent(getContext(), com.Xperia.MultipleOptions.class);
startActivity(i);
}
}
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
}
private class LogoutRequestListener extends BaseRequestListener {
public void onComplete(String response, final Object state) {
// callback should be run in the original thread,
// not the background thread
mHandler.post(new Runnable() {
public void run() {
SessionEvents.onLogoutFinish();
}
});
}
}
private class SessionListener implements AuthListener, LogoutListener {
public void onAuthSucceed() {
setImageResource(R.drawable.logout_button);
SessionStore.save(mFb, getContext());
}
public void onAuthFail(String error) {
}
public void onLogoutBegin() {
}
public void onLogoutFinish() {
SessionStore.clear(getContext());
setImageResource(R.drawable.login_button);
}
}
}
并在第startActivity(i)
行我得
The method startActivity(Intent) is undefined for the type
LoginButton.ButtonOnClickListener
有什么想法吗?
答案 0 :(得分:1)
您需要应用程序上下文才能启动新意图。
Yourclass.this.startActivity(something);
答案 1 :(得分:0)
mActivity.startActivity(i);
我认为这就是为什么你首先定义了动力学的原因;也许你只是忘了使用它。