0

When running this code the application crashes instantly.

Error: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{(...).MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

Through research I was able to find out the cause of the crash is likely to be that I name two Views before the onCreate() method has run. I could name these Views within the onCreate() method and the following listeners but then the Views cannot be public, so they would be independent from each other which would prevent my App from working as intended. Does someone have any idea on how to prevent this problem without making these Views independent from each other?

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout0);
    LayoutInflater inflater = getLayoutInflater();
    public View rectimage3 = inflater.inflate(R.layout.my_rectview, constraintLayout, false);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);

        //set Listeners
        rectimage.setOnTouchListener(new MySecOnTouchListener());
        rectimage3.setOnTouchListener(new MyOnTouchListener());
        constraintLayout.setOnDragListener(new MyDragListener());
    }

    //OnTouchListener of the first Rect
    private final class MySecOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View sview, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if(action == MotionEvent.ACTION_DOWN);
                rectimage3.setVisibility(View.VISIBLE);
                constraintLayout.addView(rectimage3);

                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(rectimage3);
                rectimage3.startDrag(data, shadowBuilder, rectimage3, 0);
                rectimage3.setVisibility(View.INVISIBLE);
                return true;
            } else{
                return false;
            }
        }
    }

    //OnTouchListener of the movable Rects
    private final class MyOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    //OnDragListener of the Layout
    class MyDragListener implements View.OnDragListener{
        @Override
        public boolean onDrag(View v, DragEvent event) {
            View view = (View) event.getLocalState();

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    view.setX(event.getX()-(view.getWidth()/2));
                    view.setY(event.getY()-(view.getHeight()/2));
                    break;
                case DragEvent.ACTION_DROP:
                    break;
            }
            return true;
        }
    }
}
vm345
  • 813
  • 12
  • 28
Argos
  • 13
  • 5
  • you should do the initialization after the layout is inflated. – Sagar Nayak Jan 17 '18 at 12:37
  • 1)THere's no reason you can't make them public if you create them in onCreate. 2)You shouldn't have any public members of an Activity class anyway. If you need to access views of an activity in other classes they should be passed in. You should not be passing around references to your Activity to access its data members. – Gabe Sechan Jan 17 '18 at 12:39
  • @GabeSechan 1) _Modifier 'public' not allowed here_ 2) How do you pass them in? – Argos Jan 18 '18 at 09:03
  • It seems you confuse creation with declaration. You can declare a variable but not create its value yet. As for number 2- as parameters to either the constructor or the function that actually needs them – Gabe Sechan Jan 18 '18 at 14:14
  • @GabeSechan ah great thanks man!! – Argos Jan 18 '18 at 14:54

3 Answers3

1

You have to bind views and access methods only inside main method or after main method.

public class MainActivity extends AppCompatActivity {
    public ConstraintLayout constraintLayout;
    LayoutInflater inflater ;
    public View rectimage3 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout0);
    inflater = getLayoutInflater();
    rectimage3 = inflater.inflate(R.layout.my_rectview, constraintLayout, false);
    final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);

    //set Listeners
    rectimage.setOnTouchListener(new MySecOnTouchListener());
    rectimage3.setOnTouchListener(new MyOnTouchListener());
    constraintLayout.setOnDragListener(new MyDragListener());
}

//OnTouchListener of the first Rect
private final class MySecOnTouchListener implements View.OnTouchListener {
    public boolean onTouch(View sview, MotionEvent motionEvent){
        int action = motionEvent.getAction();
        if(action == MotionEvent.ACTION_DOWN);
            rectimage3.setVisibility(View.VISIBLE);
            constraintLayout.addView(rectimage3);

            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(rectimage3);
            rectimage3.startDrag(data, shadowBuilder, rectimage3, 0);
            rectimage3.setVisibility(View.INVISIBLE);
            return true;
        } else{
            return false;
        }
    }
}

//OnTouchListener of the movable Rects
private final class MyOnTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent){
        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

//OnDragListener of the Layout
class MyDragListener implements View.OnDragListener{
    @Override
    public boolean onDrag(View v, DragEvent event) {
        View view = (View) event.getLocalState();

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_LOCATION:
                view.setX(event.getX()-(view.getWidth()/2));
                view.setY(event.getY()-(view.getHeight()/2));
                break;
            case DragEvent.ACTION_DROP:
                break;
        }
        return true;
    }
}

}

Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25
1

Try this

 public class MainActivity extends AppCompatActivity {
             public ConstraintLayout constraintLayout; 
             LayoutInflater inflater;  
             public View rectimage3;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       constraintLayout = (ConstraintLayout findViewById(R.id.constraint_layout0);
       inflater = getLayoutInflater();
       rectimage3 = inflater.inflate(R.layout.my_rectview, constraintLayout, false)

;

0

Try This

public class MainActivity extends AppCompatActivity {
    public ConstraintLayout constraintLayout ;
    LayoutInflater inflater = getLayoutInflater();
    public View rectimage3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);
        inflater = getLayoutInflater();
        rectimage3 = inflater.inflate(R.layout.my_rectview, 
        constraintLayout, 
       false)
       ; //set Listeners
        rectimage.setOnTouchListener(new MySecOnTouchListener());
        rectimage3.setOnTouchListener(new MyOnTouchListener());
        constraintLayout.setOnDragListener(new MyDragListener());
    }

    //OnTouchListener of the first Rect
    private final class MySecOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View sview, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if(action == MotionEvent.ACTION_DOWN);
                rectimage3.setVisibility(View.VISIBLE);
                constraintLayout.addView(rectimage3);

                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(rectimage3);
                rectimage3.startDrag(data, shadowBuilder, rectimage3, 0);
                rectimage3.setVisibility(View.INVISIBLE);
                return true;
            } else{
                return false;
            }
        }
    }

    //OnTouchListener of the movable Rects
    private final class MyOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    //OnDragListener of the Layout
    class MyDragListener implements View.OnDragListener{
        @Override
        public boolean onDrag(View v, DragEvent event) {
            View view = (View) event.getLocalState();

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    view.setX(event.getX()-(view.getWidth()/2));
                    view.setY(event.getY()-(view.getHeight()/2));
                    break;
                case DragEvent.ACTION_DROP:
                    break;
            }
            return true;
        }
    }
}
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39