我想在 main.xml 文件的底部放置一个自定义视图。以下是我的 Activity 类和我的自定义视图类(game.java):我希望游戏视图出现在 main.xml 文件的底部.请告诉我如何继续使用它....我认为这个想法是在 main.xml 中设置一个布局,然后在布局上设置自定义视图。我的主要目标是让自定义视图出现在底部main.xml 布局的一半。请帮帮我!!!!!!
//学习活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//For my button class
/*
MyButton myb = (MyButton)findViewById(R.id.mybutton1);
myb.setText("Hello Students");
myb.setTextSize(40);
**/
//For custom view
setContentView(R.layout.main);
RelativeLayout r=(RelativeLayout) findViewById(R.id.relative1);
g=new game(this,null);
ViewGroup vgroup=(ViewGroup) findViewById(R.id.main);
vgroup.addView(g);
Thread mythread=new Thread(new UpdateThread());
mythread.start();
}
Game.java(自定义视图)
public class game extends View{
private Bitmap image;
private Paint paint;
private int x=0;private int baseY = 250;
public game(Context context,AttributeSet attrs) {
super(context,attrs);
this.inflate(context, R.layout.example, null);
image=Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher));
paint =new Paint();
paint.setColor(Color.BLUE);
}
public View getlay (RelativeLayout r)
{
return r;
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(90, baseY,200,baseY, paint);
canvas.drawLine(125,baseY,125,baseY-100,paint);
canvas.drawLine(110,baseY,125,baseY-15,paint);
..................
}
)
日志猫:
06-28 22:43:59.757: E/AndroidRuntime(505): FATAL EXCEPTION: main
06-28 22:43:59.757: E/AndroidRuntime(505): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.com.learn/your.com.learn.LearningActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class your.com.learn.game
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.os.Looper.loop(Looper.java:123)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 22:43:59.757: E/AndroidRuntime(505): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:43:59.757: E/AndroidRuntime(505): at java.lang.reflect.Method.invoke(Method.java:507)
06-28 22:43:59.757: E/AndroidRuntime(505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 22:43:59.757: E/AndroidRuntime(505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 22:43:59.757: E/AndroidRuntime(505): at dalvik.system.NativeStart.main(Native Method)
06-28 22:43:59.757: E/AndroidRuntime(505): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class your.com.learn.game
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-28 22:43:59.757: E/AndroidRuntime(505): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.Activity.setContentView(Activity.java:1657)
06-28 22:43:59.757: E/AndroidRuntime(505): at your.com.learn.LearningActivity.onCreate(LearningActivity.java:38)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-28 22:43:59.757: E/AndroidRuntime(505): ... 11 more
06-28 22:43:59.757: E/AndroidRuntime(505): Caused by: java.lang.NoSuchMethodException: game(Context,AttributeSet)
06-28 22:43:59.757: E/AndroidRuntime(505): at java.lang.Class.getMatchingConstructor(Class.java:643)
06-28 22:43:59.757: E/AndroidRuntime(505): at java.lang.Class.getConstructor(Class.java:472)
06-28 22:43:59.757: E/AndroidRuntime(505): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
06-28 22:43:59.757: E/AndroidRuntime(505): ... 21 more