我创建了一个扩展表面视图并在XML中声明的类。但是在执行抛出错误时
这是我的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="com.example.canvassurfaceview.MainActivity" >
<com.example.canvasballsmoving.surfaceviewclass
android:id="@+id/surfaceView_mine"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp" />
</RelativeLayout>
这是主要活动
> public class MainActivity extends Activity {
>
> @Override
> protected void onCreate(Bundle savedInstanceState)
> {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
> //setContentView( new CanvasSurfaceView(this));
> }
}
这是班级
public class surfaceviewclass extends SurfaceView implements SurfaceHolder.Callback
{
private static final String TAG = "NEW";
public surfaceviewclass(Context context)
{
super(context);
Log.e ( TAG, " Surfacevie class constructor called ");
getHolder().addCallback(this);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.e ( TAG, "MY surface is created ");
// TODO Auto-generated method stub
Log.e ( TAG, " Height of new surface "+ getHeight());
Log.e ( TAG, " widthof new surface "+ getWidth());
}
/* All overrided functions */
}
这是错误
09-28 21:12:31.169: W/dalvikvm(1975): threadid=1: thread exiting with uncaught exception (group=0x40018578)
09-28 21:12:31.179: E/AndroidRuntime(1975): FATAL EXCEPTION: main
09-28 21:12:31.179: E/AndroidRuntime(1975): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.canvasballsmoving/com.example.canvasballsmoving.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.canvasballsmoving.surfaceviewclass
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.os.Looper.loop(Looper.java:130)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-28 21:12:31.179: E/AndroidRuntime(1975): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 21:12:31.179: E/AndroidRuntime(1975): at java.lang.reflect.Method.invoke(Method.java:507)
09-28 21:12:31.179: E/AndroidRuntime(1975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-28 21:12:31.179: E/AndroidRuntime(1975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-28 21:12:31.179: E/AndroidRuntime(1975): at dalvik.system.NativeStart.main(Native Method)
09-28 21:12:31.179: E/AndroidRuntime(1975): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.canvasballsmoving.surfaceviewclass
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-28 21:12:31.179: E/AndroidRuntime(1975): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.Activity.setContentView(Activity.java:1657)
09-28 21:12:31.179: E/AndroidRuntime(1975): at com.example.canvasballsmoving.MainActivity.onCreate(MainActivity.java:24)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-28 21:12:31.179: E/AndroidRuntime(1975): ... 11 more
09-28 21:12:31.179: E/AndroidRuntime(1975): Caused by: java.lang.NoSuchMethodException: surfaceviewclass(Context,AttributeSet)
09-28 21:12:31.179: E/AndroidRuntime(1975): at java.lang.Class.getMatchingConstructor(Class.java:643)
09-28 21:12:31.179: E/AndroidRuntime(1975): at java.lang.Class.getConstructor(Class.java:472)
09-28 21:12:31.179: E/AndroidRuntime(1975): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
09-28 21:12:31.179: E/AndroidRuntime(1975): ... 21 more
建议非常感谢
答案 0 :(得分:2)
public surfaceviewclass (Context context, AttributeSet attrs) {
super(context, attrs);
}
编辑:
这是从xml对视图进行膨胀(构造)时调用的构造函数。您声明的属性由第二个参数
提供