不能通过引用传递android中的布尔变量
Posted
技术标签:
【中文标题】不能通过引用传递android中的布尔变量【英文标题】:cannot do pass by reference for a boolean variable in android 【发布时间】:2012-06-06 14:27:19 【问题描述】:我是touchevent listner,它记录了触摸,我使用viewpager进行swping..我们知道java的缺点是按值传递,我仍在尝试在匿名内部类中传递布尔值。在 switch stmt 中更改的值不会保留在 switch stmt 之外。 flag2 值。有人对此有解决方案吗?我的代码如下
parent.setOnTouchListener(new OnTouchListener()
public boolean onTouch(View v, MotionEvent event)
//flag2=!flag2;
switch(event.getAction())
case MotionEvent.ACTION_MOVE:
awesomePager.requestDisallowInterceptTouchEvent(true);
//flag2=false;
break;
case MotionEvent.ACTION_UP:
awesomePager.requestDisallowInterceptTouchEvent(true);
if(flag)
upperdock.setClickable(false);
upperdock.bringToFront();
tocparent.bringToFront();
tocbottom.bringToFront();
upperdock.setVisibility(RelativeLayout.VISIBLE);
tocparent.setVisibility(LinearLayout.VISIBLE);
tocbottom.setVisibility(LinearLayout.VISIBLE);
flag=false;
// obj.one(event.getAction());
else
parent.bringToFront();
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
flag=true;
//obj.one(event.getAction());
flag2=true;
break;
case MotionEvent.ACTION_CANCEL:
awesomePager.requestDisallowInterceptTouchEvent(false);
flag2=false;
break;
return flag2;
);
public class SimpleViewPagerforandroidActivity extends Activity /** Called when the activity is first created. */ boolean flag2=true; //rest of the code
请帮助...需要它..
package com.beanie.samples.viewpager;
//set of imports
public class SimpleViewPagerforAndroidActivity extends Activity
/** Called when the activity is first created. */
volatile boolean flag2=true;
AwesomePagerAdapter awe1;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
cxt = this;
data = new ArrayList<WebView>();
awesomePager = (ViewPager) findViewById(R.id.viewPager);
awesomePager.setAdapter(new AwesomePagerAdapter(this,data));
Bundle extras = getIntent().getExtras();
Boolean dock2checked = extras.getBoolean("dock1");
if(dock2checked)
Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
toast1.show();
else
Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
toast1.show();
awesomePager.setOffscreenPageLimit(11);
//
web0 = new WebView(cxt);
web0.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings0 = web0.getSettings();
webSettings0.setjavascriptEnabled(true);
web1 = new WebView(cxt);
web1.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings1 = web1.getSettings();
webSettings1.setJavaScriptEnabled(true);
web2 = new WebView(cxt);
web2.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings2 = web2.getSettings();
webSettings2.setJavaScriptEnabled(true);
web3 = new WebView(cxt);
web3.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings3 = web3.getSettings();
webSettings3.setJavaScriptEnabled(true);
web4 = new WebView(cxt);
web4.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings4 = web4.getSettings();
webSettings4.setJavaScriptEnabled(true);
web5 = new WebView(cxt);
web5.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings5 = web5.getSettings();
webSettings5.setJavaScriptEnabled(true);
web6 = new WebView(cxt);
web6.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings6 = web6.getSettings();
webSettings6.setJavaScriptEnabled(true);
web7 = new WebView(cxt);
web7.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings7 = web7.getSettings();
webSettings7.setJavaScriptEnabled(true);
web8 = new WebView(cxt);
web8.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings8 = web8.getSettings();
webSettings8.setJavaScriptEnabled(true);
web9 = new WebView(cxt);
web9.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings9 = web9.getSettings();
webSettings9.setJavaScriptEnabled(true);
web10 = new WebView(cxt);
web10.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings10 = web10.getSettings();
webSettings10.setJavaScriptEnabled(true);
data.add(web0);
data.add(web1);
data.add(web2);
data.add(web3);
data.add(web4);
data.add(web5);
data.add(web6);
data.add(web7);
data.add(web8);
data.add(web9);
data.add(web10);
tocparent=(LinearLayout) findViewById(R.id.tocparent);
dock2=(Button)findViewById(R.id.button1);
//dock2.setVisibility(Button.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
topmost=(RelativeLayout) findViewById(R.id.topmost);
parent=(RelativeLayout) findViewById(R.id.rl1);
upperdock=(RelativeLayout) findViewById(R.id.upperdock);
tocparent=(LinearLayout) findViewById(R.id.tocparent);
tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
web0.loadUrl("javascript:fhhf()");
parent.setOnTouchListener(new OnTouchListener()
public boolean onTouch(View v, MotionEvent event)
//flag2=!flag2;
switch(event.getAction())
case MotionEvent.ACTION_MOVE:
awesomePager.requestDisallowInterceptTouchEvent(true);
//flag2=false;
break;
case MotionEvent.ACTION_UP:
awesomePager.requestDisallowInterceptTouchEvent(true);
if(flag)
upperdock.setClickable(false);
upperdock.bringToFront();
tocparent.bringToFront();
tocbottom.bringToFront();
upperdock.setVisibility(RelativeLayout.VISIBLE);
tocparent.setVisibility(LinearLayout.VISIBLE);
tocbottom.setVisibility(LinearLayout.VISIBLE);
flag=false;
// obj.one(event.getAction());
else
parent.bringToFront();
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
flag=true;
//obj.one(event.getAction());
flag2=true;
break;
case MotionEvent.ACTION_CANCEL:
awesomePager.requestDisallowInterceptTouchEvent(false);
flag2=false;
//Toast.makeText(getApplicationContext(), s, 220).show();
//obj.one(event.getAction());
break;
/* if(event.getAction()==MotionEvent.ACTION_UP)
flag2=true;
else if(event.getAction()==MotionEvent.ACTION_CANCEL)
flag2=false;
*/
return flag2;
);
awesomePager.setOnPageChangeListener(new OnPageChangeListener()
public void onPageSelected(int arg0)
//Toast toast1 = Toast.makeText(getBaseContext(), "PageSelected"+ arg0, 5000);
//Toast1.show();
if(arg0==0)
//web0.loadUrl("javascript:fhhf()");
// awesomePager.getChildAt(arg0 - 1);
web0.loadUrl("javascript:fhhf1()");
web0.loadUrl("javascript:fhhf()");
else if(arg0==1)
web1.loadUrl("javascript:fhhf1()");
web1.loadUrl("javascript:fhhf()");
else if(arg0==3)
web3.loadUrl("javascript:fhhf1()");
//Toast toast2 = Toast.makeText(getBaseContext(), "fun"+ arg0, 5000);
//Toast2.show();
else if(arg0==5)
web5.loadUrl("javascript:fhhf()");
else if(arg0==7)
web7.loadUrl("javascript:fhhf()");
else if(arg0==9)
web9.loadUrl("javascript:fhhf()");
public void onPageScrolled(int arg0, float arg1, int arg2)
public void onPageScrollStateChanged(int arg0)
// TODO Auto-generated method stub
int o = awesomePager.getCurrentItem();
WebView temp = (WebView) awesomePager.getChildAt(o);
if(arg0 == 1)
temp.loadUrl("javascript:fhhf1()");
else if(arg0 == 0)
temp.loadUrl("javascript:fhhf()");
);
public boolean fun1()
return true;
public boolean fun2()
return false;
private class AwesomePagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener
private List<WebView> data;
private Context ctx1;
public AwesomePagerAdapter(Context ctx, List<WebView> data)
this.ctx1 = ctx;
this.data = data;
@Override
public int getCount()
return NUM_AWESOME_VIEWS;
@Override
public Object instantiateItem(View collection, int position)
data.get(position).setBackgroundColor(R.color.black);
return data.get(position);
@Override
public void destroyItem(View collection, int position, Object view)
((ViewPager) collection).removeView((WebView) view);
@Override
public boolean isViewFromObject(View view, Object object)
return view==((WebView)object);
@Override
public void finishUpdate(View arg0)
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1)
@Override
public Parcelable saveState()
return null;
@Override
public void startUpdate(View arg0)
public void onPageScrollStateChanged(int position)
public void onPageScrolled(int arg0, float arg1, int arg2)
// TODO Auto-generated method stub
public void onPageSelected(int position)
提前致谢
【问题讨论】:
哪个值没有改变? flag2 值.. 在类中全局声明为 true 什么匿名内部类?你不发一个。但是,非静态匿名内部类应该能够更改类中字段的值,但不能更改方法范围的变量,除非它们是最终的(这不会帮助您更改布尔值)。 如果你想通过引用传递像boolean
这样的原始变量,你必须使用包装对象lile Boolean
...
Java 中的 anything 不能通过引用传递。
【参考方案1】:
首先在 SimpleViewPagerforAndroidActivity 中创建一个方法,如下所示,如果您不想返回任何内容,也可以使用 void
boolean method(Boolean flag)
flag2=flag;
return flag2;
在其他类中创建一个对象,如下所示
SimpleViewPagerforAndroidActivity abc=new SimpleViewPagerforAndroidActivity();
在ontouch事件中switch语句调用方法由
abc.method(false or true);
这将更新你的 flag2 并且你可以通过在日志中打印来检查
Log.d("flag2",""+abc.flag2);
【讨论】:
我也试过这个。因为对象在 java 中有引用类型。但这太有效了..拿了一个布尔数组并尝试了它..这也有效..有什么东西可以同时修复viewpager的滑动和touchListener的触摸效果..我的意思是在滑动时它不能采取touchListener动作? 我已经签入日志并得到了 true 值,如果没有得到则为 parfacr,而不是显示你的整个代码【参考方案2】:也许尝试强制它在 UI 线程上运行?
IE:
public void passBoolean(final boolean boolToPass)
runOnUiThread(new Runnable()
public void run()
//Do whatever here on the UI thread
);
【讨论】:
以上是关于不能通过引用传递android中的布尔变量的主要内容,如果未能解决你的问题,请参考以下文章