0

我想在 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
4

2 回答 2

0

除了上面的答案,我还必须添加“AttributeSet attr”参数,因此游戏视图构造函数被命名为

公共游戏(上下文上下文,AttributeSet attr)

现在视图出现在 XML 布局中。将其放在底部是下一个问题。

于 2012-06-28T23:52:58.907 回答
0

使您的 main.xml 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Some others widgets here -->

    <yourpackagename.Game
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

并打电话sentContentView(R.layout.main)

于 2012-06-28T00:35:40.090 回答