首先我想请问一下:
我在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应该如何设置???
还请大侠们赐教!!!!

解决方案 »

  1.   

    错误图片大图网络地址:
    http://www.eoeandroid.com/data/attachment/forum/201108/28/175713yxlooujlxt7zm4ml.png
    帖子图片若看不清的可查看,大侠们一定要多多赐教啊!!!
      

  2.   

    context传你的MyScrollView2所在的Activity的引用
      

  3.   

    lay=LayoutInflater.from(mcontext);
    改成
    lay=LayoutInflater.from(context);
      

  4.   


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

  5.   


    你是要显示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);    
         } 
        }    
      

  6.   

    我并没有用代码初始化过MyScrollView 对象,我只是在xml布局文件中,直接加入了这个自定义控件,然后运行报错时才知道,程序执行的是带2个参数的构造函数<com.todream.HongXiTai.MyScrollView2
    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文件,实际代码中并没有初始化对象
      

  7.   

    不知道你的布局文件Button有没在ViewGroup内如果 有要先移除一下,或你直接把整个layout加到 MyScrollView2内            ViewGroup vparent= (ViewGroup)btnBack22.getParent();
                vparent.removeAllViews(); 
      

  8.   

    我想在MyScrollView2这个类中获得那个Button控件,而这个Button控件是在main.xml定义的,button的位置和scrollview无关,我想进行的操作是,如果我findViewById得到这个button后根据scrollview滑动事件随时设置button的位置及它的其他属性
      

  9.   

    我想在MyScrollView2这个类中获得那个Button控件,而这个Button控件是在main.xml定义的,button的位置和scrollview无关,我想进行的操作是,如果我findViewById得到这个button后根据scrollview滑动事件随时设置button的位置及它的其他属性,但是现在的问题是没法在MyScrollView2这个类中获得button,运行报错了btnBack22.getParent()是运行不了的,因为现在button没得到,属于空引用,不知道怎么得到
      

  10.   


    那你的scrollview和Button在哪里显示呢 
      

  11.   

    在另一个类中public class HongXiTaiActivity extends Activity {}在这个类的onCreat(){}方法中加载main.xml,因为在这个类中无法监听scrollview的正在滑动事件,只能在自定义里写相应的操作
      

  12.   


    那何必要那么麻烦 直接在HongXiTaiActivity把btnBack设成静态的,或MyScrollView2的构造函数内传个btnBack参数给MyScrollView2,MyScrollView2滑动时修改他的属性。
      

  13.   

    你是布局文件出错了吧。你在布局文件中找下,顺便问下,你是不是把自定义的View写入到布局文件中啊。如果这样的话,你的注意自定义view的两参构造函数哦
      

  14.   

    问题是我没有用代码初始化过MyScrollView2对象,我只是在main.xml中直接用自定义的控件,它默认调用的有2个参数的构造函数,所以没办法通过构造函数传button
      

  15.   

    判断可能是传值有问题,但我也不知道它是什么时候传值的,因为我没有用代码初始化MyScrollView2对象,只是将自定义的控件写在main.xml中,它确实是默认调用有2个参数的构造函数,我的构造函数中这样写直接获得:
    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时出错的,还请大侠看看谢谢!
      

  16.   


    既然控件都还没显示 你可以在定一个变量记录button的状态  等MyScrollView2创建了显示button时再读取他
      

  17.   

    ScrollView 必须包含一个LinearyLayout,所有的子控件都放在 LinearyLayout