在ProfilePictureView上设置用户标识时为空对象引用

时间:2016-11-17 04:27:43

标签: android facebook-sdk-4.0

我正在尝试使用导航标题中的用户配置文件ID设置profilePictureView,但每次尝试我都会:

Attempt to invoke virtual method 'void com.facebook.login.widget.ProfilePictureView.setProfileId(java.lang.String)' on a null object reference

setNavigationDrawer方法发生错误。

然而,我可以在" setUserProfilePhoto_Name_ID()方法上设置它。

我的主要活动课程如下。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setNavigationDrawer();
        setUserProfilePhoto_Name_ID();
    }

    public void setNavigationDrawer(){
        Profile profile = Profile.getCurrentProfile();
        navProfileImage = (ProfilePictureView) findViewById(R.id.nav_profilePhoto);
        navProfileImage.setProfileId(profile.getId());
        toggle = new ActionBarDrawerToggle(this,drawerLayout, R.string.open, R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        navigationView.setNavigationItemSelectedListener(this);
    }


    public void setUserProfilePhoto_Name_ID(){
        Profile profile = Profile.getCurrentProfile();
        profilePicture.setProfileId(profile.getId());
        //navProfileImage = (ImageView) findViewById(R.id.nav_profilePhoto);
        nameView.setText(profile.getName());
        idView.setText(profile.getId());
    }

    @OnClick(R.id.AlgorithmsButton)
    public void onAlgorithmsButtonClick(){
        initializeCategoryScreen(ALGORITHMS_INFO);
        //Toast.makeText(this, "Clicked on algo button", Toast.LENGTH_LONG).show();
    }

    @OnClick(R.id.DataStructuresButton)
    public void onDataStructuresButtononClick(){
        initializeCategoryScreen(DATASTRUCTURES_INFO);
    }

    @OnClick(R.id.SoftwareDesignPatternButton)
    public void onSoftwareDesignonClick(){
        initializeCategoryScreen(SOFTWAREDESIGN_INFO);
    }

    public void initializeCategoryScreen(int info) {
        Bundle extraInfo = new Bundle();
        Intent i = new Intent(this, CategoryScreen.class);
        extraInfo.putInt("categoryScreen", info);
        i.putExtras(extraInfo);
        startActivity(i);
    }


    private void goToLoginScreen() {
        LoginManager.getInstance().logOut();
        Intent intent = new Intent(this, LoginActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


    public void logoutUser(DialogInterface.OnClickListener view){

        GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/{user-id}/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse graphResponse) {
                if(graphResponse!=null){
                    FacebookRequestError error =graphResponse.getError();
                    if(error!=null){
                        Log.e(TAG, error.toString());
                    }else {
                        finish();
                    }
                }
            }
        });
        Log.d(TAG,"Executing revoke permissions with graph path" + delPermRequest.getGraphPath());
        delPermRequest.executeAsync();
        LoginManager.getInstance().logOut();
        goToLoginScreen();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.Home_menu_item:
                Toast.makeText(MainActivity.this,"Clicked on home",Toast.LENGTH_LONG).show();
                break;

            case R.id.Profile_menu_item:
                Toast.makeText(MainActivity.this, "Clicked on Profile", Toast.LENGTH_LONG).show();
                Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
                startActivity(profileIntent);
                break;

            case R.id.Quizzes_menu_item:
                Toast.makeText(MainActivity.this, "Clicked on Quizzes", Toast.LENGTH_LONG).show();
                break;

            case R.id.Community_menu_item:
                Toast.makeText(MainActivity.this, "Clicked on Community", Toast.LENGTH_LONG).show();
                break;

            case R.id.About_menu_item:
                Toast.makeText(MainActivity.this, "Clicked on About", Toast.LENGTH_LONG).show();
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(aboutIntent);
                break;

            case R.id.Logout_menu_item:
                Toast.makeText(MainActivity.this, "Clicked on Logout", Toast.LENGTH_LONG).show();
                //Alert Dialog to ask user if they are sure they want to logout.
                new AlertDialog.Builder(this)
                        .setTitle("Logout")
                        .setMessage("Are you sure you want to logout?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               // logoutUser(this);
                                goToLoginScreen();
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        })
                        .setIcon(R.drawable.ic_priority_high_black_24dp)
                        .show();
                break;
        }
        //Code below checks if the drawer is open,
            //if it is open and something is clicked, it will close.
        DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawerLayout);
        if(dl.isDrawerOpen(GravityCompat.START)){
            dl.closeDrawer(GravityCompat.START);
        }

        return false;
    }

    @Override
    public void onResume(){
        super.onResume();

        Profile profile = Profile.getCurrentProfile();
        if(profile == null){
            Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(loginIntent);
        }
    }

}

0 个答案:

没有答案