Java 使用静态嵌套类引发的"bug"

来源:互联网 发布:通过php将base64 编辑:程序博客网 时间:2024/06/06 04:57

前言

昨天在做android app时要实现一个功能,在三个界面之间能过滑动来返回上一个界面,如:第三个界面右滑返回第二个界面,第二个界面右滑返回第一个界面。

1.布局文件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="cn.sehzh.statictester.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="first page" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="go second page" /></RelativeLayout>
activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="cn.sehzh.statictester.SecondActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="second page" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="go third page" /></RelativeLayout>

activity_third.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="cn.sehzh.statictester.ThirdActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="third page" /></RelativeLayout>
2.Activity

package cn.sehzh.statictester;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent;intent = new Intent(MainActivity.this, SecondActivity.class);MainActivity.this.startActivity(intent);}});}}

SecondActivity

package cn.sehzh.statictester;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;public class SecondActivity extends Activity {CommonGestureDetector mGestureDetector;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);mGestureDetector = new CommonGestureDetector(this, this);findViewById(R.id.button2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent;intent = new Intent(SecondActivity.this, ThirdActivity.class);SecondActivity.this.startActivity(intent);}});}@Overridepublic boolean onTouchEvent(MotionEvent event) {return mGestureDetector.onTouchEvent(event);}}
ThirdActivity

package cn.sehzh.statictester;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;public class ThirdActivity extends Activity {CommonGestureDetector mGestureDetector;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_third);mGestureDetector = new CommonGestureDetector(this, this);}@Overridepublic boolean onTouchEvent(MotionEvent event) {return mGestureDetector.onTouchEvent(event);}}

类CommonGestureDetector

package cn.sehzh.statictester;import android.app.Activity;import android.content.Context;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;public class CommonGestureDetector extends GestureDetector {static Activity mActivity;static class MyGestureListener implements OnGestureListener {@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e2.getX() - e1.getX() > 50) {mActivity.finish();return true;}return false;}}public CommonGestureDetector(Context context, Activity activity) {// super(context, new CommonGestureListener(activity));super(context, new MyGestureListener());mActivity = activity;}}
出现的问题:

直接从第二个界面右滑能回到第一个界面,但是先从第三个界面右滑回到第二个界面,在第二个界面右滑就不能回到第一个界面了。

原因:

在CommonGestureDetector类中,static class MyGestureListener为静态嵌套类,mActivity为静态成员,在我开启第三个界面后,mActivity就一直引用的是第三个界面的Activity,在第三个界面右滑后,对ThirdActivity类调用了finish()方法,此时回到第二个界面,由于并不会调用第二个界面(SecondActivity)的onCreate()方法,mActivity引用的还是第三个界面的Activity,所以这时候右滑就没有作用。

解决办法:

CommonGestureDetector修改为:

package cn.sehzh.statictester;import android.app.Activity;import android.content.Context;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;public class CommonGestureDetector extends GestureDetector {public CommonGestureDetector(Context context, Activity activity) {super(context, new CommonGestureListener(activity));}}class CommonGestureListener implements OnGestureListener {private Activity mActivity;protected static final float FLIP_DISTANCE = 50;public CommonGestureListener(Activity mActivity) {super();this.mActivity = mActivity;}@Overridepublic boolean onDown(MotionEvent e) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e2.getX() - e1.getX() > FLIP_DISTANCE) {mActivity.finish();return true;}return false;}}







0 0
原创粉丝点击