例外:类无法强制转换为android.app.activity

时间:2013-12-18 12:16:39

标签: android

我想在我的Android应用程序中扩展另一个类而不是Activity。但是当我在清单文件中初始化它后运行应用程序时,它会显示异常class cannot be cast to android.app.activity如何解决这个问题? 编辑: 这是我的类文件SessionLoginSampleActivity.java,它调用了FacebookSignIn

 public class SessionLoginSampleActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }        }
        updateView();
    }

    @Override
    public void onStart() {
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }
    private void updateView() {
        Session session = Session.getActiveSession();
        if (session.isOpened()) {
            startActivityForResult(new Intent(SessionLoginSampleActivity.this, FacebookSignIn.class), 100);
            }
        else{
            onClickLogin();      }    }

   private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        } else {
            Session.openActiveSession(this, true, statusCallback);
        }    }

    private void onClickLogout() {
        Session session = Session.getActiveSession();
        if (!session.isClosed()) {
            session.closeAndClearTokenInformation();
        }    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            updateView();
        }    } }

FacebookSignIn.java

    public class FacebookSignIn extends FacebookFragment{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     fetchUserInfo();
     if(databaseExists()!=false)
     {
            if(!checkValidUser()!=true){
                Intent intent = new Intent(getActivity(),UserAccount.class);
                 startActivity(intent);
                /*Sending some arguments*/ 
                Bundle bundle = new Bundle();
                  bundle.putString("UserName",strUserName);
                  bundle.putString("Id", strEmailId);
                  intent.putExtras(bundle);
                this.startActivity(intent);
        }}}
private boolean databaseExists(){
    SQLiteDatabase checkDB = null;     
    try {       
        String myPath = DB_PATH + DATABASE_NAME; 
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
        if(checkDB != null){
            System.out.println("DataBase Exists");
            checkDB.close(); 
            return true;
        }else{  
            //database does not exist  
            return false;
        }     
    } catch (Exception e){
        //database does not exist        
        return false;   
    }}  

private Boolean checkValidUser(){
    Cursor cursor = null;
    try{
        cursor = database.rawQuery("SELECT loginId, emailAddress FROM UserDetails WHERE loginId = '"+NAME+"' AND emailAddress = '"+ID+"'", null);
        if (cursor != null ) {
            if  (cursor.moveToFirst()){
                strUserName = cursor.getString(cursor.getColumnIndex("loginId"));
                strEmailId = cursor.getString(cursor.getColumnIndex("emailAddress"));
                        }}          
    }catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }finally {
      if(cursor != null && !cursor.isClosed()) cursor.close();
    }
    return true;
}    
private void fetchUserInfo() {
    final Session currentSession = getSession();
    if (currentSession != null && currentSession.isOpened()) {
        if (currentSession != userInfoSession) {
            Request request = Request.newMeRequest(currentSession, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser me, Response response) {
                    if (currentSession == getSession()) {
                        user = me;                           
                        updateUI();
                    }
                    if (response.getError() != null) {
                        //loginButton.handleError(response.getError().getException());
                    }}});
            Bundle parameters = new Bundle();
            parameters.putString(FIELDS, REQUEST_FIELDS);
            request.setParameters(parameters);
            Request.executeBatchAsync(request);
            userInfoSession = currentSession;
        }
    } else {
        user = null;
    }}

private void updateUI() {
    if (!isAdded()) {
        return;
    }
    if (isSessionOpen()) {
       // connectedStateLabel.setTextColor(getResources().getColor(R.color.com_facebook_usersettingsfragment_connected_text_color));
       // connectedStateLabel.setShadowLayer(1f, 0f, -1f,
              //  getResources().getColor(R.color.com_facebook_usersettingsfragment_connected_shadow_color));

        if (user != null) {
            ImageRequest request = getImageRequest();
            if (request != null) {
                URI requestUrl = request.getImageUri();
                // Do we already have the right picture? If so, leave it alone.
                //if (!requestUrl.equals(connectedStateLabel.getTag())) {
                    if (user.getId().equals(userProfilePicID)) {

                    } else {
                        ImageDownloader.downloadAsync(request);
                    }}}}} 

private ImageRequest getImageRequest() {
    ImageRequest request = null;
    try {
        ImageRequest.Builder requestBuilder = new ImageRequest.Builder(
                getActivity(),
                ImageRequest.getProfilePictureUrl(
                        user.getId(),
                        getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_width),
                        getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_height)));

        request = requestBuilder.setCallerTag(this)
                .setCallback(
                        new ImageRequest.Callback() {
                            @Override
                            public void onCompleted(ImageResponse response) {
                                processImageResponse(user.getId(), response);
                            }})
                .build();
    } catch (URISyntaxException e) {
    }
    return request;
}

private void processImageResponse(String id, ImageResponse response) {
    if (response != null) {
        Bitmap bitmap = response.getBitmap();
        if (bitmap != null) {
            BitmapDrawable drawable = new BitmapDrawable(FacebookSignIn.this.getResources(), bitmap);
            drawable.setBounds(0, 0,
                    getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_width),
                    getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_height));
            userProfilePic = drawable;
            userProfilePicID = id;

        } } }}

2 个答案:

答案 0 :(得分:0)

您不需要在Android Manifest中声明除Activity之外的类。

仅添加扩展Activity的类。像FragmentActivity,Activity等。

希望这有帮助

答案 1 :(得分:0)

可能UserAccount您尝试以活动开始,实际上不是Activity

活动应通过某个中间子类直接或间接扩展android.app.Activity