不知道应用程序崩溃的原因

时间:2015-04-29 13:38:10

标签: android

我已经编写了一个代码,但它一直在崩溃,我不知道从这一点开始该做什么。这是我的代码:

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);

        }
    }
}

这是我的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)

我希望有人为我找到解决方案。我会非常感激。

提前谢谢

2 个答案:

答案 0 :(得分:4)

  

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

从pi级别11可以获得

setX,您可能会将代码运行在较旧的设备上,或者运行在具有较旧sdk的模拟器上。你肯定应该查看ViewCompat类,它返回了一些这些方法的端口。例如。

ViewCompat.setX(m_view, 40) 

并非所有版本的android都支持后端移植,但至少你要避开NoSuchMethodErrorException。另一种方法是明确检查SDK_INT

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

答案 1 :(得分:1)

@SuppressLint("NewApi")

删除您应该看到的警告。 setX适用于API 11 +。

一种可能的解决方案是使用LayoutParams并设置边距:

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