0

I am adding and removing view dynammically, as follows:

Codes:

    view_share_dialog = findViewById(R.id.view_share_coloring);

    case R.id.btn_share_coloring:

        if(btn.isChecked())
        {

            CloseOpenedView(btn);
            AnimUtils.slideToLeft_ColorView(view_share_dialog, mContext);
            view_share_dialog.bringToFront();
            mDrawView.newLine(android.R.color.transparent);
            Toast.makeText(getApplicationContext(), "FRONT", Toast.LENGTH_SHORT).show();
        }
        else
        {
            img.bringToFront();
            AnimUtils.slideToRight_ColorView(view_share_dialog, mContext);                  
            mDrawView.newLine(Color.parseColor(allColors[lastSelectedCol]));
            ((RelativeLayout)view_share_dialog.getParent()).removeView(view_share_dialog);

            Toast.makeText(getApplicationContext(), "GONE", Toast.LENGTH_SHORT).show();
        }
        break;

// these are the buttons of choices for sharing in the View
public void shareButtonClickable(boolean clickable)
{
    findViewById(R.id.btn_share_copy).setClickable(clickable);  //line 467
    findViewById(R.id.btn_share_email).setClickable(clickable);
    findViewById(R.id.btn_share_facebook_c).setClickable(clickable);
    findViewById(R.id.btn_share_photo_album).setClickable(clickable);
}

Logcat:

06-22 01:52:36.571: E/AndroidRuntime(15096): Caused by: java.lang.reflect.InvocationTargetException
06-22 01:52:36.571: E/AndroidRuntime(15096):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 01:52:36.571: E/AndroidRuntime(15096):    at java.lang.reflect.Method.invoke(Method.java:515)
06-22 01:52:36.571: E/AndroidRuntime(15096):    at android.view.View$1.onClick(View.java:3964)
06-22 01:52:36.571: E/AndroidRuntime(15096):    ... 12 more
06-22 01:52:36.571: E/AndroidRuntime(15096): Caused by: java.lang.NullPointerException
06-22 01:52:36.571: E/AndroidRuntime(15096):    at com.abc.abc.ColoringPageActivity.shareButtonClickable(ColoringPageActivity.java:467)
06-22 01:52:36.571: E/AndroidRuntime(15096):    at com.abc.abc.ColoringPageActivity.onClickToggle_coloring(ColoringPageActivity.java:528)
06-22 01:52:36.571: E/AndroidRuntime(15096):    ... 15 more

Question:

For the first time pressing the btn_share_coloring button, the view can be animated from right of screen to the center. And then when the btn_share_coloring is pressed again (i.e. unchecked), the view can also be animated from center to the right, with the Toast "Gone" can be properly shown. Yet when the user further press again for the btn, a fatal NPE will be resulted.

While the View can be properly being removed, how could the view be inflated again properly?

Thanks!

pearmak
  • 4,979
  • 15
  • 64
  • 122
  • line 467 is now newly indicated in the codes above~ it is one of the buttons inside the view – pearmak Jun 22 '14 at 03:47
  • Looks like you should readd `view_share_dialog`. Something like `((RelativeLayout)view_share_dialog.getParent()).addView(view_share_dialog);` – matiash Jun 22 '14 at 03:51
  • i would like to ask since there are buttons inside the view, do i need to add the buttons inside the view one by one apart from the view? or adding the view will together adding all the things inside it? – pearmak Jun 22 '14 at 04:22
  • If you remove view X and then add it again, it will be back with whatever it contained before. – matiash Jun 22 '14 at 04:37

0 Answers0