首页 > 解决方案 > 我在与 facebook 集成并创建帐户时遇到这些运行时错误,如何解决它们?

问题描述

嗨,我在运行时遇到这些错误,当我第一次运行应用程序时与 facebook 集成时,它给了我这个错误

无效范围用户朋友

然后我尝试更改电子邮件、basic_profile 等权限,但现在当我运行应用程序时,它会登录但保持不变LoginActivity并且没有转到我MainActivity刚刚登录的 facebook 按钮已更改为注销。之后,现在我的应用程序在我运行它和调试它时崩溃,它会给我NullPointerExceptions在 setText 用户配置文件的名称和图像的方法中的错误。

并且我在我的应用程序中为自定义帐户创建了一个本地数据库,但是NullPointerExceptions当我单击以单击以创建我自己创建的帐户和isInputField方法时,当数据发布到数据库时它也会给我错误,但我自己创建的方法在该活动中不起作用但是当我尝试登录然后isInputField方法工作,但是当我在插入电子邮件和密码后单击登录按钮时,我的应用程序崩溃并在verifyFromSqlite方法中给出错误

找不到这样的列 user_password

但是当我在数据库中检查我的表时,有user_friend一列。

所以我应该怎么做才能消除所有这些错误,请帮助我,我很担心,因为这是我最后一年的项目。我附上了我的所有代码LoginActivity,,SignUpActivityMainActivity数据库类

LoginActivityjava类代码

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    private final AppCompatActivity activity=LoginActivity.this;
    private NestedScrollView nestedScrollView;

    private TextInputLayout textInputLayoutEmail;
    private TextInputLayout textInputLayoutPassword;

    private TextInputEditText textInputEditTextEmail;
    private TextInputEditText textInputEditTextPassword;

    private AppCompatButton login;
    private AppCompatTextView forgottonPassword;
    private AppCompatTextView linkRegiter;

    private InputValidation inputValidation;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);

        fbLogin();

        if (getSupportActionBar()!=null)
        {
            getSupportActionBar().hide();
        }
        initViews();
        initListeners();
        initObjects();


    }


    protected void fbLogin(){
        callbackManager=CallbackManager.Factory.create();
        accessTokenTracker=new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken currentToken) {

            }
        };
        profileTracker=new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                nextActivity(newProfile);
            }
        };
        accessTokenTracker.startTracking();
        profileTracker.startTracking();

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);

        FacebookCallback<LoginResult> callback= new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Profile profile= Profile.getCurrentProfile();
                nextActivity(profile);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        };

        loginButton.setReadPermissions("user_friends");
        loginButton.registerCallback(callbackManager, callback);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Profile profile= Profile.getCurrentProfile();
        nextActivity(profile);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode,resultCode,data);
    }

    private void nextActivity(Profile profile){
        if (profile!=null){
            Intent main = new Intent(LoginActivity.this,MainActivity.class);
            main.putExtra("name",profile.getFirstName());
            main.putExtra("imageUrl",profile.getProfilePictureUri(100,80)).toString();
            //   main.putExtra("id", profile.getId());
            startActivity(main);

        }
    }

    private void initViews(){
        nestedScrollView= (NestedScrollView) findViewById(R.id.nestedScrollView) ;
        textInputLayoutEmail= (TextInputLayout) findViewById(R.id.textInputLayoutEmail);

        textInputLayoutPassword= (TextInputLayout) findViewById(R.id.textInputLayoutPassword);

        textInputEditTextEmail= (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
        textInputEditTextPassword= (TextInputEditText) findViewById(R.id.textInputEditTextPassword);
        Log.e("textinputedittextmail",textInputEditTextEmail.toString());

        login=(AppCompatButton) findViewById(R.id.login);
        linkRegiter= (AppCompatTextView) findViewById(R.id.linkRegister);
    }

    private void initListeners(){
        login.setOnClickListener(this);
        linkRegiter.setOnClickListener(this);
    }

    public  void initObjects(){
        databaseHelper=new DatabaseHelper(activity);
        inputValidation=new InputValidation(activity);

    }

    @Override
    public void onClick(View v) {


        switch (v.getId()){
            case R.id.login:
                Log.v("abc", textInputEditTextEmail.getText().toString());
                verifyFromSQLite();
                break;
            case R.id.linkRegister:
                Intent intentRegister= new Intent(getApplicationContext(),SignupActivity.class);
                startActivity(intentRegister);
                break;

        }

    }

    private void verifyFromSQLite(){
        if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, "Please Enter Valid Email")){

            return;}
        if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, "Please Enter Valid Email"))
            return;
        if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, "Please Enter Valid Password"))
            return;
        if (databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim(),
                textInputEditTextPassword.getText().toString().trim())){

            Intent accountIntent= new Intent(activity, MainActivity.class);
            accountIntent.putExtra("Email",textInputEditTextEmail.getText().toString().trim());
            emptyInputEditText();
            startActivity(accountIntent);
        }else {
            Snackbar.make(nestedScrollView, "Please Enter Valid Email or Password", Snackbar.LENGTH_LONG).show();
        }
    }

    private void emptyInputEditText(){
        textInputEditTextEmail.setText("");
        textInputEditTextPassword.setText("");
    }
}

SignUpjava类代码:

public class SignupActivity extends AppCompatActivity implements View.OnClickListener {

    private final AppCompatActivity activity=SignupActivity.this;

    private NestedScrollView nestedScrollView;

    private TextInputLayout textInputLayoutFirstName;
    private TextInputLayout textInputLayoutLastName;
    private TextInputLayout textInputLayoutEmail;
    private TextInputLayout textInputLayoutPassword;
    private TextInputLayout textInputLayoutConfirmPassword;

    private TextInputEditText textInputEditTextFirstName;
    private TextInputEditText textInputEditTextLastName;
    private TextInputEditText textInputEditTextEmail;
    private TextInputEditText textInputEditTextPassword;
    private TextInputEditText textInputEditTextConfirmPassword;

    private AppCompatButton appCompatButtonCreateAccount;
    private AppCompatTextView linklogin;

    private InputValidation inputValidation;
    private DatabaseHelper databaseHelper;
    private User user;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        if (getSupportActionBar()!=null)
        {
            getSupportActionBar().hide();
        }

        initViews();
        initListeners();
        initObjects();
    }

    private void initViews(){
        nestedScrollView=(NestedScrollView) findViewById(R.id.nestedScrollView);

        textInputLayoutFirstName= (TextInputLayout) findViewById(R.id.textInputLayoutFirstName);
        textInputLayoutLastName= (TextInputLayout) findViewById(R.id.textInputLayoutLastName);
        textInputLayoutEmail= (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
        textInputLayoutPassword= (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
        textInputLayoutConfirmPassword= (TextInputLayout) findViewById(R.id.textInputLayoutConfirmPassword);

        textInputEditTextEmail= (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
        textInputEditTextPassword= (TextInputEditText) findViewById(R.id.textInputEditTextPassword);

        appCompatButtonCreateAccount=(AppCompatButton) findViewById(R.id.appCompatButtonCreateAccount);
        linklogin= (AppCompatTextView) findViewById(R.id.linklogin);
    }

    private void initListeners(){
        appCompatButtonCreateAccount.setOnClickListener(this);
        linklogin.setOnClickListener(this);
    }

    public  void initObjects(){
        databaseHelper=new DatabaseHelper(activity);
        inputValidation=new InputValidation(activity);
        user=new User();

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.appCompatButtonCreateAccount:
                postDataToSQLite();
                break;
            case R.id.linklogin:
                finish();
                break;
        }

    }


    private void postDataToSQLite(){
        if (!inputValidation.isInputEditTextFilled(textInputEditTextFirstName, textInputLayoutFirstName, "Please Enter Full Name"))
            return;
        if (!inputValidation.isInputEditTextFilled(textInputEditTextLastName, textInputLayoutLastName, "Please Enter Full Name"))
            return;
        if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, "Please Enter Valid Email"))
            return;
        if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail,"Please Enter Valid Email" ))
            return;
        if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, "Please Enter Password"))
            return;

        if (!databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim())){
            user.setFirstName(textInputEditTextFirstName.getText().toString().trim());
            user.setLastName(textInputEditTextLastName.getText().toString().trim());
            user.setEmail(textInputEditTextEmail.getText().toString().trim());
            user.setEmail(textInputEditTextPassword.getText().toString().trim());

            databaseHelper.addUser(user);

            Snackbar.make(nestedScrollView, "Registration Successful",Snackbar.LENGTH_LONG).show();
            emptyInputEditText();
        }else {
            Snackbar.make(nestedScrollView, "Already a member Please Login", Snackbar.LENGTH_LONG).show();
       }
    }

    private void emptyInputEditText(){
        textInputEditTextFirstName.setText("");
        textInputEditTextLastName.setText("");
        textInputEditTextEmail.setText("");
        textInputEditTextPassword.setText("");
        textInputEditTextConfirmPassword.setText("");
    }
}

databaseHelper java类代码

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="userManager.db";
    public static final String TABLE_USER="user";

    public static final String COLUMN_USER_ID="user_id";
    public static final String COLUMN_USER_FIRSTNAME="user_firstName";
    public static final String COLUMN_USER_LASTNAME="user_lastName";
    public static final String COLUMN_USER_EMAIL="user_email";
    public static final String COLUMN_USER_PASSWORD="user_password";

    public String CREATE_USER_TABLE="CREATE TABLE "+TABLE_USER+"("
            + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+ COLUMN_USER_FIRSTNAME + " TEXT,"
            + COLUMN_USER_LASTNAME+ " TEXT,"+COLUMN_USER_EMAIL + " TEXT,"+COLUMN_USER_PASSWORD
            +" TEXT" + ")";
    private String DROP_USER_TABLE="DROP TABLE IF EXISTS "+TABLE_USER;


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DROP_USER_TABLE);
        onCreate(db);

    }

    public void addUser(User user){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_USER_FIRSTNAME, user.getFirstName());
        values.put(COLUMN_USER_LASTNAME, user.getLastName());
        values.put(COLUMN_USER_EMAIL, user.getEmail());
        values.put(COLUMN_USER_PASSWORD, user.getPassword());

        db.insert(TABLE_USER, null,values);
        db.close();
    }

    public boolean checkUser(String email){
        String[] columns={COLUMN_USER_ID};

        SQLiteDatabase db=this.getWritableDatabase();
        String selection= COLUMN_USER_EMAIL+ " = ?";
        String [] selectionArgs={email };

        Cursor cursor= db.query(TABLE_USER,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null);
        int cursorCount=cursor.getCount();
        cursor.close();
        db.close();
        if (cursorCount>0){
            return true;
        }
        return false;

    }

    public boolean checkUser(String email , String password){
        String[] columns={
                COLUMN_USER_ID
        };

        SQLiteDatabase db=this.getWritableDatabase();
        String selection= COLUMN_USER_EMAIL+ " = ?" + " AND " + COLUMN_USER_PASSWORD+" =?";
        String [] selectionArgs ={ email, password };

        Cursor cursor= db.query(TABLE_USER,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null);
        int cursorCount=cursor.getCount();
        cursor.close();
        db.close();
        if (cursorCount>0){
            return true;
        }
        return false;

    }
}

inputValidation java类代码

public class InputValidation {

    private Context context;

    public InputValidation(Context context){

        this.context=context;
    }

    public boolean isInputEditTextFilled(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message){
        String value = "";
        value=textInputEditText.getText().toString().trim();
        if (value.isEmpty()){
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText);
            return false;
        }else {
            textInputLayout.setErrorEnabled(false);
        }
        return true;

    }

    public boolean isInputEditTextEmail(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message){
        String value = textInputEditText.getText().toString().trim();
        if (value.isEmpty()|| !Patterns.EMAIL_ADDRESS.matcher(value).matches()){
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText);
            return false;
        }else {
            textInputLayout.setErrorEnabled(false);
        }
        return true;

    }

    public boolean isInputEditTextMatches(TextInputEditText textInputEditText, TextInputEditText textInputEditText2 , TextInputLayout textInputLayout, String message){
        String value1 = "";
        value1=textInputEditText.getText().toString().trim();
        String value2 = "";
        value2=textInputEditText.getText().toString().trim();
        if (!value1.contentEquals(value2)){
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText);
            return false;
        }else {
            textInputLayout.setErrorEnabled(false);
        }
        return true;

    }

    private void hideKeyboardFrom(View view){
        InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}

主Activity类java代码

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private ShareDialog shareDialog;
    private String name=" ";
    private String imageUrl=" ";
    //private String id=" ";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fabShare();
        getAndSetData();
        new MainActivity.DownloadImage((ImageView)findViewById(R.id.profilePic));


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if (savedInstanceState==null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
                    new ActivityActivity()).commit();
            navigationView.setCheckedItem(R.id.nav_activity);
        }
    }

    private void fabShare(){
        FloatingActionButton fab =(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareLinkContent content= new ShareLinkContent.Builder().build();
                shareDialog.show(content);
            }
        });
    }

    private void getAndSetData(){
        Bundle inBundle=getIntent().getExtras();
        name=inBundle.get("name").toString();
        imageUrl=inBundle.get("imageUrl").toString();
        //id=inBundle.get("id").toString();

        TextView yourName=(TextView) findViewById(R.id.yourName);
        TextView yourId=(TextView) findViewById(R.id.yourId);
        yourName.setText(name);
        //yourId.setText(id);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_activity) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
                    new ActivityActivity()).commit();
        } else if (id == R.id.nav_history) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
                    new HistoryActivity()).commit();

        } else if (id == R.id.nav_settings) {

        } else if (id == R.id.nav_logout){
            LoginManager.getInstance().logOut();
            Intent login=new Intent(MainActivity.this,LoginActivity.class);
            startActivity(login);
            finish();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public class DownloadImage extends AsyncTask<String,Void ,Bitmap>{
        ImageView bmImage;

        public DownloadImage(ImageView bmImage){
            this.bmImage=bmImage;
        }

        @Override
        protected Bitmap doInBackground(String... urls) {
            String urlDisplay=urls[0];
            Bitmap bitmap=null;
            try {
                InputStream inputStream= new java.net.URL(urlDisplay).openStream();
                bitmap= BitmapFactory.decodeStream(inputStream);

            }catch (Exception e){
                Log.e("Error",e.getMessage());
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            bmImage.setImageBitmap(bitmap);
        }
    }
}

我没有附加xml代码,因为它跨越了用户类中的限制和用户类,只有用于数据库列的 getter 和 setter。

标签: javaandroidfacebookandroid-studiofacebook-javascript-sdk

解决方案


推荐阅读