Milly Corsh
Milly Corsh

Reputation: 59

Don't know why the app is crashing

I have written a code, but it keeps on crashing, i don't know what to do from this point onwards. Here is my code:

public class MainActivity extends Activity {
    int windowwidth;
    int screenCenter;
    int x_cord, y_cord, x, y;
    int Likes = 0;
    RelativeLayout parentView;
    float alphaValue = 0;
    private Context m_context;

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mainlayout);
        m_context = MainActivity.this;

        parentView = (RelativeLayout) findViewById(R.id.layoutview);
        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        screenCenter = windowwidth / 2;
        int[] myImageList = new int[] { R.drawable.cats, R.drawable.baby1, R.drawable.sachin,
                R.drawable.cats, R.drawable.puppy };

        for (int i = 0; i < 5; i++) {
            LayoutInflater inflate = (LayoutInflater) m_context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View m_view = inflate.inflate(R.layout.custom_layout, null);
            ImageView m_image = (ImageView) m_view.findViewById(R.id.sp_image);
            LinearLayout m_topLayout = (LinearLayout) m_view.findViewById(R.id.sp_color);
            LinearLayout m_bottomLayout = (LinearLayout) m_view.findViewById(R.id.sp_linh);
            // final RelativeLayout myRelView = new RelativeLayout(this);
            m_view.setLayoutParams(new LayoutParams((windowwidth - 80), 450));
            m_view.setX(40);
            m_view.setY(40);
            m_view.setTag(i);
            m_image.setBackgroundResource(myImageList[i]);

            if (i == 0) {
                m_view.setRotation(-1);
            } else if (i == 1) {
                m_view.setRotation(-5);

            } else if (i == 2) {
                m_view.setRotation(3);

            } else if (i == 3) {
                m_view.setRotation(7);

            } else if (i == 4) {
                m_view.setRotation(-2);

            } else if (i == 5) {
                m_view.setRotation(5);

            }

            // ADD dynamically like button on image.
            final Button imageLike = new Button(m_context);
            imageLike.setLayoutParams(new LayoutParams(100, 50));
            imageLike.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
            imageLike.setX(20);
            imageLike.setY(-250);
            imageLike.setAlpha(alphaValue);
            m_topLayout.addView(imageLike);

            // ADD dynamically dislike button on image.
            final Button imagePass = new Button(m_context);
            imagePass.setLayoutParams(new LayoutParams(100, 50));
            imagePass.setBackgroundDrawable(getResources().getDrawable(R.drawable.dislike));

            imagePass.setX(260);
            imagePass.setY(-300);
            imagePass.setAlpha(alphaValue);
            m_topLayout.addView(imagePass);

            // Click listener on the bottom layout to open the details of the
            // image.
            m_bottomLayout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(m_context, DetailsActivity.class));

                }
            });

            // Touch listener on the image layout to swipe image right or left.
            m_topLayout.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    x_cord = (int) event.getRawX();
                    y_cord = (int) event.getRawY();

                    m_view.setX(x_cord - screenCenter + 40);
                    m_view.setY(y_cord - 150);
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = (int) event.getX();
                        y = (int) event.getY();
                        Log.v("On touch", x + " " + y);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        x_cord = (int) event.getRawX(); // Updated for more
                                                        // smoother animation.
                        y_cord = (int) event.getRawY();
                        m_view.setX(x_cord - x);
                        m_view.setY(y_cord - y);
                        // m_view.setY(y_cord-y);
                        // y_cord = (int) event.getRawY();
                        // m_view.setX(x_cord - screenCenter + 40);
                        // m_view.setY(y_cord - 150);
                        if (x_cord >= screenCenter) {
                            m_view.setRotation((float) ((x_cord - screenCenter) * (Math.PI / 32)));
                            if (x_cord > (screenCenter + (screenCenter / 2))) {
                                imageLike.setAlpha(1);
                                if (x_cord > (windowwidth - (screenCenter / 4))) {
                                    Likes = 2;
                                } else {
                                    Likes = 0;
                                }
                            } else {
                                Likes = 0;
                                imageLike.setAlpha(0);
                            }
                            imagePass.setAlpha(0);
                        } else {
                            // rotate
                            m_view.setRotation((float) ((x_cord - screenCenter) * (Math.PI / 32)));
                            if (x_cord < (screenCenter / 2)) {
                                imagePass.setAlpha(1);
                                if (x_cord < screenCenter / 4) {
                                    Likes = 1;
                                } else {
                                    Likes = 0;
                                }
                            } else {
                                Likes = 0;
                                imagePass.setAlpha(0);
                            }
                            imageLike.setAlpha(0);
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        x_cord = (int) event.getRawX();
                        y_cord = (int) event.getRawY();

                        Log.e("X Point", "" + x_cord + " , Y " + y_cord);
                        imagePass.setAlpha(0);
                        imageLike.setAlpha(0);

                        if (Likes == 0) {
                            // Log.e("Event Status", "Nothing");
                            m_view.setX(40);
                            m_view.setY(40);
                            m_view.setRotation(0);
                        } else if (Likes == 1) {
                            // Log.e("Event Status", "Passed");
                            parentView.removeView(m_view);
                        } else if (Likes == 2) {

                            // Log.e("Event Status", "Liked");
                            parentView.removeView(m_view);
                        }
                        break;
                    default:
                        break;
                    }
                    return true;
                }
            });

            parentView.addView(m_view);

        }
    }
}

Here is my LogCat

04-29 13:33:37.516: E/AndroidRuntime(8930): FATAL EXCEPTION: main
04-29 13:33:37.516: E/AndroidRuntime(8930): java.lang.NoSuchMethodError: android.view.View.setX
04-29 13:33:37.516: E/AndroidRuntime(8930):     at com.example.tinderview_demo.MainActivity.onCreate(MainActivity.java:55)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.os.Looper.loop(Looper.java:150)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at android.app.ActivityThread.main(ActivityThread.java:4389)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at java.lang.reflect.Method.invoke(Method.java:507)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-29 13:33:37.516: E/AndroidRuntime(8930):     at dalvik.system.NativeStart.main(Native Method)

I hope anybody finds a solution for me. I would be so grateful.

Thankyou in advance

Upvotes: 1

Views: 75

Answers (2)

Simas
Simas

Reputation: 44118

@SuppressLint("NewApi")

removes the warning that you are supposed to see. setX is available for API 11+.

One possible solution would be to use the LayoutParams and set the margins:

LinearLayout.LayoutParams params = new LinearLayout
        .LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = 50; // X
params.topMargin = 60; // Y
m_view.setLayoutParams(params);

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157447

java.lang.NoSuchMethodError: android.view.View.setX

setX is available from api level 11, and you are probably run the code an older device or on an emulator with an older sdk. You should definitely look into the ViewCompat class, which back port some of those methods. E.g.

ViewCompat.setX(m_view, 40) 

not all of them are back-ported on all version of android, but at least you avoid the NoSuchMethodErrorException. Another way is to check explicitly against the SDK_INT

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES. HONEYCOMB) {
      // run starting from honeycomb
}

Upvotes: 4

Related Questions