本帖最后由 diablox0147 于 2011-06-06 02:01:56 编辑

解决方案 »

  1.   

    布局这样:<?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="fill_parent"
        >
    <TextView  
        style = "@style/SpecialText"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
        <Button
            android:id="@+id/top"
            android:nextFocusUp="@+id/bottom"
            style = "@style/SpecialText"
            android:layout_width = "fill_parent"
            android:layout_height="wrap_content"
            android:text = "OK"
        />  
        <Button
            android:id="@+id/bottom"
            android:nextFocusDown="@+id/top"
            android:layout_width = "fill_parent"
            android:layout_height="wrap_content"
            android:text = "NotOk"
        />
        <ImageView
            android:id="@+id/image"
            android:src="@drawable/icon"
            android:layout_width = "wrap_content"
            android:layout_height="wrap_content"
        />
        <com.enseignement.AndroidApp.CustomDrawableView
            android:id="@+id/custom_drawable_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </LinearLayout>
    Activity中这样处理下:public class App extends Activity {    private CustomDrawableView cusView;        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //CustomDrawableView cusView = new CustomDrawableView(this); 从xml中获得对象就不用new了  
            setContentView(R.layout.main);
           // setContentView(cusView); 如果换成这个然后把xml里的去掉就不会允许失败//用这个其他控件不显示吗?
            cusView = (CustomDrawableView )findViewById(R.id.custom_drawable_view);
        }   
    }
      

  2.   

    按3楼的试了后还是一样的结果,,,
    The application has stopped unexpectedly. Please try again.虽然失败了,不过3楼可以解释下为什么要这么处理吗??
    在Activity里获得实例有什么重要性?
      

  3.   

    把错发上来,这样描述没什么用
    Activity获得实例一般都这样没有什么解释的
      

  4.   

    在模拟器里打开后就是那个错误, The application has stopped unexpectedly. Please try again.你没明白,我的问题是,为什么你觉得在Activity里获得实例就可以解决问题了呢,,? 而不是怎么获得这个事例
      

  5.   

    因为你的代码有问题:
    setContentView(cusView);了之后我想问下其他控件还能显示?要是lz的目的就是这么处理那么main.xml就没什么意义可以删掉了setContentView(R.layout.main);
    的话就像我上面那样获得实例,而lz却
    CustomDrawableView cusView = new CustomDrawableView(this);
    那么这个对象和main.xml的CustomDrawableView 是2个对象。我只是让你这样处理下,我没觉得一定能解决你的问题,
    The application has stopped unexpectedly. Please try again.这个错误信息发了和没法一样,基本上所有的异常崩溃都会报这个错
      

  6.   


    我知道那样会是2个实例,那样做不过是试试单独显示一个CustomDwavableView会不会运行失败,结果没有任何问题, 而加入到main.XML后却会失败
      

  7.   

    xml 中是静态创建,试一试重载之后能否可以    public xxxView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }    public xxxView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
      

  8.   

    cusView = (CustomDrawableView )findViewById(R.id.custom_drawable_view);子类就这样写,否则得不到xml
      

  9.   

    个人理解,是通过 xml 中静态创建的时候,需要用到这两个建构函数吧,所以才出现如果不重载就不行的情况;具体更深入的我也说不上来