活动意图无法按下按钮

时间:2018-06-02 16:48:07

标签: java android android-intent android-activity

Hello Stackoverflow社区! 最近,一个奇怪的错误发生在我身上。它是我的应用程序中的一个按钮。当我按它时没有任何事情发生。没有错误或崩溃但是Intent也没有运行。没有发生到DeleteAccountActivity的过渡。我不知道为什么会这样。我使用的意图非常简单(没有通过)。请帮帮我

AccountSettingsActivity.java

public class AccountSettingsActivity extends AppCompatActivity {

    private static final String TAG = "AccountSettingsActivity";
    private static final int ACTIVITY_NUM = 4;
    private  String user_id;




    private Context mContext;
    public SectionsStatePagerAdapter pagerAdapter;
    private ViewPager mViewPager;
    private RelativeLayout mRelativeLayout;
    private Button mDelete;
    User mUser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountsettings);
        mContext = AccountSettingsActivity.this;
        Log.d(TAG, "onCreate: started.");
        mViewPager = (ViewPager) findViewById(R.id.viewpager_container);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relLayout1);
        mDelete = (Button) findViewById(R.id.btnDelete);
        User mUser = new User();
        setupSettingsList();
        setupBottomNavigationView();
        setupFragments();
        getIncomingIntent();

        //setup the backarrow for navigating back to "ProfileActivity"
        ImageView backArrow = (ImageView) findViewById(R.id.backArrow);
        backArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: navigating back to 'ProfileActivity'");
                finish();

                mDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View view) {

                    Intent intent = new Intent(mContext, DeleteAccountActivity.class);
                    startActivity(intent);
                }
                });
                }
        });
    }


    private void deleteAccounts(){


   /*         DatabaseReference deleteUser = FirebaseDatabase.getInstance().getReference("users").child(user_id);
            DatabaseReference deleteUserPhotos = FirebaseDatabase.getInstance().getReference("user_photos").child(user_id);
            DatabaseReference deleteUserPhotoComments = FirebaseDatabase.getInstance().getReference("comments").child(user_id);

            deleteUser.removeValue();
            deleteUserPhotos.removeValue();
            deleteUserPhotoComments.removeValue();*/

            // getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

        FirebaseDatabase.getInstance().getReference()
                .child(getString(R.string.dbname_users))
             //   .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(mUser.getUser_id())
                .removeValue();

     /*   FirebaseDatabase.getInstance().getReference()
                .child(getString(R.string.dbname_followers))
                .child(mUser.getUser_id())
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .removeValue(); */


    }

    private void getIncomingIntent(){
        Intent intent = getIntent();

        if(intent.hasExtra(getString(R.string.selected_image))
                || intent.hasExtra(getString(R.string.selected_bitmap))){

            //if there is an imageUrl attached as an extra, then it was chosen from the gallery/photo fragment
            Log.d(TAG, "getIncomingIntent: New incoming imgUrl");
            if(intent.getStringExtra(getString(R.string.return_to_fragment)).equals(getString(R.string.edit_profile_fragment))){

                if(intent.hasExtra(getString(R.string.selected_image))){
                    //set the new profile picture
                    FirebaseMethods firebaseMethods = new FirebaseMethods(AccountSettingsActivity.this);
                    firebaseMethods.uploadNewPhoto(getString(R.string.profile_photo), null, 0,
                            intent.getStringExtra(getString(R.string.selected_image)), null);
                }
                else if(intent.hasExtra(getString(R.string.selected_bitmap))){
                    //set the new profile picture
                    FirebaseMethods firebaseMethods = new FirebaseMethods(AccountSettingsActivity.this);
                    firebaseMethods.uploadNewPhoto(getString(R.string.profile_photo), null, 0,
                            null,(Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap)));
                }

            }

        }

        if(intent.hasExtra(getString(R.string.calling_activity))){
            Log.d(TAG, "getIncomingIntent: received incoming intent from " + getString(R.string.profile_activity));
            setViewPager(pagerAdapter.getFragmentNumber(getString(R.string.edit_profile_fragment)));
        }
    }

    private void setupFragments(){
        pagerAdapter = new SectionsStatePagerAdapter(getSupportFragmentManager());
        pagerAdapter.addFragment(new EditProfileFragment(), getString(R.string.edit_profile_fragment)); //fragment 0
        pagerAdapter.addFragment(new SignOutFragment(), getString(R.string.sign_out_fragment)); //fragment 1
     //   pagerAdapter.addFragment(new DeleteAccountFragment(), "Delete Account");
    }

    public void setViewPager(int fragmentNumber){
        mRelativeLayout.setVisibility(View.GONE);
        Log.d(TAG, "setViewPager: navigating to fragment #: " + fragmentNumber);
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setCurrentItem(fragmentNumber);
    }

    private void setupSettingsList(){
        Log.d(TAG, "setupSettingsList: initializing 'Account Settings' list.");
        ListView listView = (ListView) findViewById(R.id.lvAccountSettings);

        ArrayList<String> options = new ArrayList<>();
        options.add(getString(R.string.edit_profile_fragment)); //fragment 0
        options.add(getString(R.string.sign_out_fragment)); //fragement 1
  //      options.add("Delete Account");

        ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, options);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "onItemClick: navigating to fragment#: " + position);
                setViewPager(position);
            }
        });

    }


    /**
     * BottomNavigationView setup
     */
    private void setupBottomNavigationView(){
        Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
        BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
        BottomNavigationViewHelper.enableNavigation(mContext, this,bottomNavigationViewEx);
        Menu menu = bottomNavigationViewEx.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);
    }


}

DeleteAccountActivity.java

public class DeleteAccountActivity extends AppCompatActivity {

    Button yesButton;
    Button cancelButton;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        setContentView(R.layout.activity_delete_account);

        yesButton = (Button) findViewById(R.id.btnDelete2);
        cancelButton = (Button) findViewById(R.id.btnDelete3);

        yesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAllData();

                Intent intent = new Intent(mContext, RegisterActivity.class);
                startActivity(intent);
            }
        });
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, AccountSettingsActivity.class);

        }
    });
    }

        public void deleteAllData(){

            DatabaseReference deleteUser = FirebaseDatabase.getInstance().getReference()
                    .child("users")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

            DatabaseReference deleteUserPhotos = FirebaseDatabase.getInstance().getReference()
                    .child("user_photos")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

            DatabaseReference deleteUserPhotoComments = FirebaseDatabase.getInstance().getReference()
                    .child("user_account_settings")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

            DatabaseReference deleteUserFollowing = FirebaseDatabase.getInstance().getReference()
                    .child("following")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

            DatabaseReference deleteUserFollowers = FirebaseDatabase.getInstance().getReference()
                    .child("followers")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

            deleteUser.removeValue();
            deleteUserPhotos.removeValue();
            deleteUserPhotoComments.removeValue();
            deleteUserFollowing.removeValue();
            deleteUserFollowers.removeValue();


    }

    }

4 个答案:

答案 0 :(得分:1)

问题:

Press backarrow (But this will finish the activity)
Press mdelete to trigger the intent. (so no way to trigger intent)

因为当您按向后箭头时,您正在mdelete上设置侦听器,该侦听器实际上具有触发意图的代码,而finish将终止该活动,因此无法触发意图

解决方案:分离听众

backArrow.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: navigating back to 'ProfileActivity'");
        finish();    
        }
});

mDelete.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View view) {

    Intent intent = new Intent(mContext, DeleteAccountActivity.class);
    startActivity(intent);
}
});

答案 1 :(得分:0)

请将clickListeners设置为此更改行:

ImageView backArrow = (ImageView) findViewById(R.id.backArrow);
backArrow.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: navigating back to 'ProfileActivity'");
        finish();
        }
});

mDelete.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View view) {

        Intent intent = new Intent(mContext, DeleteAccountActivity.class);
        startActivity(intent);
    }
});

答案 2 :(得分:0)

我认为你的onCreate()方法应如下所示:

id

答案 3 :(得分:0)

我可以通过浏览看到的一个简单的事情是,你的按钮的onClick监听器应该带你去删除活动在onClick监听器的后退箭头内分配。 因此,只有当您按下后退按钮时,您的删除按钮被分配了一个点击监听器,它仍然会分配,因为您已经调用了     完(); 关于活动

更改

backArrow.setOnClickListener(new View.OnClickListener() { 
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating back to 'ProfileActivity'");
finish();

mDelete.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
Intent intent = new Intent(mContext, DeleteAccountActivity.class); 
startActivity(intent); 
} });

}});

backArrow.setOnClickListener(new View.OnClickListener() { 
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating back to 'ProfileActivity'");
finish();

}});

mDelete.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
Intent intent = new Intent(mContext, DeleteAccountActivity.class); 
startActivity(intent); 
} });

希望这有帮助。