0

I want to create an new image by touchscreen but it shows errors which I don't know reasons. It shows

The constructor ImageView(LinearLayout) is undefined

This is main activity:

public class MainActivity extends Activity  {   

    public LinearLayout screenlayout;

    public void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_main); 

        final LinearLayout screenlayout= (LinearLayout)findViewById(R.id.screenlayout);

        screenlayout.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        int x_cord = (int) event.getX();
                        int y_cord = (int) event.getY();

                        AddPicture aa = new AddPicture();
                        aa.addNewer(screenlayout, x_cord, y_cord);                      

                        break;
                    case MotionEvent.ACTION_MOVE:                   
                        break;
                    default:
                        break;
                    }
                    return true;         
                   }
            });             
    }            
}

This is AddPicture Class:

public class AddPicture extends MainActivity{


    public void addNewer(LinearLayout screenlayout,int vtx,int vty) {

        ImageView i = new ImageView(screenlayout);  

        i.setImageResource(R.drawable.ball);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));                    
        screenlayout.addView(i);        
        setContentView(screenlayout); 
    }
}
Tyler
  • 17,669
  • 10
  • 51
  • 89
Dang Hoang
  • 161
  • 11

1 Answers1

0

I made a little change in your code:

This is main activity:

public class MainActivity extends Activity {

public static MainActivity mInstance = null;  // new edit

    public LinearLayout screenlayout;

    public void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

    mInstance = this;  // new edit

        setContentView(R.layout.activity_main); 

        final LinearLayout screenlayout= (LinearLayout)findViewById(R.id.screenlayout);

        screenlayout.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        int x_cord = (int) event.getX();
                        int y_cord = (int) event.getY();

                        AddPicture aa = new AddPicture(MainActivity.this);
                        screenlayout.addView(aa.addNewer(x_cord, y_cord));  // edit                   
                        setContentView(screenlayout);
                        break;
                    case MotionEvent.ACTION_MOVE:                   
                        break;
                    default:
                        break;
                    }
                    return true;         
                   }
            });             
    }            
    }

This is AddPicture Class:

// if a simple class then do not extend with Activity

public class AddPicture { 

private Context mContext;

public AddPicture(Context context)
{
  this.mContext = context;
}
    public ImageView addNewer(int vtx,int vty) 
    {    
        ImageView i = new ImageView(mContext);  // edit

        i.setImageResource(R.drawable.ic_launcher);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));       

        return i;
    }
    }

Edited : Its working now