首先我想请问一下:
我在MyScrollView这个类中能获得main.xml上的Button吗?
其中MyScrollView 是这么定义的:
public class MyScrollView extends ScrollView {}
我的做法是这样的:
public class MyScrollView2 extends ScrollView {public int a;public int b;View mTopView;View mCenterView;private MyApp appData;Context mcontext;LayoutInflater lay;View v;Button btnBack22;public MyScrollView2(Context context) {super(context);
appData=(MyApp)this.getContext();mcontext=context;lay=LayoutInflater.from(mcontext);v=lay.inflate(R.layout.main, null);btnBack22=(Button)v.findViewById(R.id.btnBack);}
public MyScrollView2(Context context, AttributeSet attrs) {super(context, attrs);mcontext=context;lay=LayoutInflater.from(mcontext);v=lay.inflate(R.layout.main, null);btnBack22=(Button)v.findViewById(R.id.btnBack);}
public MyScrollView2(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);mcontext=context;lay=LayoutInflater.from(mcontext);v=lay.inflate(R.layout.main, null);btnBack22=(Button)v.findViewById(R.id.btnBack);}}
通过日志报错是调用有2个参数的构造函数, v=lay.inflate(R.layout.main, null);这一句出错:试问 lay=LayoutInflater.from(mcontext);中的context应该如何设置???
还请大侠们赐教!!!!
解决方案 »
- gridview 怎么显示网络图片???
- 在电脑上编程如何获取模拟器上手机QQ的好友列表?
- 一个有关Android的页面的跳转的问题
- Gallery的问题,请高手指点
- Android自带截屏并通过Wifi发送的工具名?
- 各位朋友,我想用手机点灯一个LED灯,要怎么操作。大神给点思路。
- 手机游戏中怎么把多张png图片合并成一张png
- Android Layout的问题
- 准备把一款游戏移植到android上,请大家出点子
- 执行adb shell, 提示: error: no devices/emulators found..., 为什么呢?
- 有人用过HttpConnector类么?
- listview.getview遇到奇怪的问题????
http://www.eoeandroid.com/data/attachment/forum/201108/28/175713yxlooujlxt7zm4ml.png
帖子图片若看不清的可查看,大侠们一定要多多赐教啊!!!
改成
lay=LayoutInflater.from(context);
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
MyScrollView myView; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myView= new MyScrollView(MainActivity.this);
}
}
你是要显示Button在ScrollView吗?你好像都没addview。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyScrollView2(this));
;
}
public class MyScrollView2 extends ScrollView
{
public int a; public int b;
View mTopView;
View mCenterView;
//private MyApp appData;
Context mcontext;
LayoutInflater lay;
View v;
Button btnBack22;
public MyScrollView2(Context context)
{
super(context);
//appData=(MyApp)this.getContext();
mcontext=context;
lay=LayoutInflater.from(context);
v=lay.inflate(R.layout.main, null);
btnBack22=(Button)v.findViewById(R.id.btnBack);
ViewGroup vparent= (ViewGroup)btnBack22.getParent();
vparent.removeAllViews();
this.addView(btnBack22);
}
public MyScrollView2(Context context, AttributeSet attrs)
{
super(context, attrs);
mcontext=context;
lay=LayoutInflater.from(context);
v=lay.inflate(R.layout.main, null);
btnBack22=(Button)v.findViewById(R.id.btnBack);
ViewGroup vparent= (ViewGroup)btnBack22.getParent();
vparent.removeAllViews();
this.addView(btnBack22);
}
public MyScrollView2(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mcontext=context;
lay=LayoutInflater.from(context);
v=lay.inflate(R.layout.main, null);
btnBack22=(Button)v.
findViewById(R.id.btnBack);
ViewGroup vparent= (ViewGroup)btnBack22.getParent();
vparent.removeAllViews();
this.addView(btnBack22);
}
}
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fadingEdge="vertical" android:id="@+id/sc1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"></LinearLayout>
</com.todream.HongXiTai.MyScrollView2>
上面是我的xml文件,实际代码中并没有初始化对象
vparent.removeAllViews();
那你的scrollview和Button在哪里显示呢
那何必要那么麻烦 直接在HongXiTaiActivity把btnBack设成静态的,或MyScrollView2的构造函数内传个btnBack参数给MyScrollView2,MyScrollView2滑动时修改他的属性。
lay=LayoutInflater.from(mcontext);v=lay.inflate(R.layout.main, null);btnBack22=(Button)v.findViewById(R.id.btnBack);
报错报的是v=lay.inflate(R.layout.main, null);一句有问题,好像lay并未获得错误信息是:
java.lang.reflect.InvocationTargetException
调用方法或构造方法所抛出异常的经过检查的异常。 配置没有配置好,但不知该怎么做
我的main.xml是这么写的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/m_root_nav">
<Button android:text="后退" android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="@+id/layoutTopSc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="0.565" >
<com.todream.HongXiTai.MyScrollView
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fadingEdge="vertical" android:id="@+id/sc1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/imgbig8"
android:src="@drawable/s8_nav_rtpic" />
</LinearLayout> </com.todream.HongXiTai.MyScrollView>
</LinearLayout>
<LinearLayout android:id="@+id/layoutStub" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" android:layout_weight="0.435">
<ViewStub android:id="@+id/stubView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout="@layout/viewstublayout" >
</ViewStub>
</LinearLayout>
<LinearLayout android:id="@+id/layoutCenter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="0.435">
<LinearLayout android:id="@+id/layoutCenterSc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="0.845">
<com.todream.HongXiTai.MyScrollViewCenter
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fadingEdge="vertical" android:id="@+id/scCenter">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/itemC0"
android:src="@drawable/m_s0" />
</LinearLayout> </com.todream.HongXiTai.MyScrollViewCenter>
</LinearLayout>
<LinearLayout android:id="@+id/layoutBelowSc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="0.155">
<com.todream.HongXiTai.MyScrollView2
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fadingEdge="vertical" android:id="@+id/sc2">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/itemB0"
android:src="@drawable/m_s5_1" />
</LinearLayout> </com.todream.HongXiTai.MyScrollView2>
</LinearLayout>
</LinearLayout></LinearLayout>
报错第98行有错误,是<com.todream.HongXiTai.MyScrollView2...这一行
也就是在加载MyScrollView2时出错的,还请大侠看看谢谢!
既然控件都还没显示 你可以在定一个变量记录button的状态 等MyScrollView2创建了显示button时再读取他