0

我最近开始在 Eclipse 中使用 android。我必须保持模型-视图-控制器 (MVC) 结构。

我的目标只是在屏幕上显示一个矩形框。我现在得到的是一个有四个屏幕(主菜单和链接到它的其他三个屏幕)的应用程序。盒子实现适用于这三个屏幕(板)之一

board.java 代码

package com.example.connectfour;

import com.example.connectfour.model.Gaps;
import com.example.connectfour.view.ConnectFourView;

imports[..]

public class Board extends Activity {

    private Button btnExit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        connectfourView = new ConnectFourView(this);

        setContentView(R.layout.board);


        btnExit = (Button)findViewById(R.id.btn4);
        btnExit.setOnClickListener(new OnClickListener(){

            public void onClick(View v){
                Intent startMain = new Intent(Board.this, MainActivity.class);
                startActivity(startMain);
            }
        });
    }

}

板子.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:background="#48D9AB">


        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Play"
        tools:context=".Board" />

            <Button 
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="Exit"        />
</RelativeLayout>

ConnectFourView.java 代码

package com.example.connectfour.view;

import com.example.connectfour.model.Gaps;

imports[..]

public class ConnectFourView extends View {
    public ConnectFourView(Context context) {
        super(context);
        setMinimumWidth(100);
        setMinimumHeight(20);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = Math.max(getSuggestedMinimumWidth(),widthMeasureSpec);
            int height = Math.max(getSuggestedMinimumHeight(), heightMeasureSpec);
            setMeasuredDimension(width, height);
    }


    protected void onDraw(Canvas canvas){
        canvas.drawColor(Color.WHITE);
    }

}

我已经在 board.xml 文件中尝试了以下代码,我相信它会将它们组合在一起,但是当插入它时,我的应用程序会向 logcat 发送 30 多个错误。

     <example.connectfour.view
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>

注意:由于这是我的第一篇 android 帖子,对于任何缺失/额外的代码,我提前感到抱歉。谢谢

编辑 TanjaV。我在 logcat 中遇到的错误(这些都是在将上述代码添加到 board.xml 文件时出现的。

11-20 19:14:56.446: E/AndroidRuntime(630): FATAL EXCEPTION: main
11-20 19:14:56.446: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.Board}: android.view.InflateException: Binary XML file line #23: Error inflating class com.example.connectfour.view.ConnectFourView
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.os.Looper.loop(Looper.java:123)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 19:14:56.446: E/AndroidRuntime(630):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 19:14:56.446: E/AndroidRuntime(630):  at java.lang.reflect.Method.invoke(Method.java:521)
11-20 19:14:56.446: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 19:14:56.446: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 19:14:56.446: E/AndroidRuntime(630):  at dalvik.system.NativeStart.main(Native Method)
11-20 19:14:56.446: E/AndroidRuntime(630): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.example.connectfour.view.ConnectFourView
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-20 19:14:56.446: E/AndroidRuntime(630):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.Activity.setContentView(Activity.java:1647)
11-20 19:14:56.446: E/AndroidRuntime(630):  at com.example.connectfour.Board.onCreate(Board.java:28)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-20 19:14:56.446: E/AndroidRuntime(630):  ... 11 more
11-20 19:14:56.446: E/AndroidRuntime(630): Caused by: java.lang.NoSuchMethodException: ConnectFourView(Context,AttributeSet)
11-20 19:14:56.446: E/AndroidRuntime(630):  at java.lang.Class.getMatchingConstructor(Class.java:660)
11-20 19:14:56.446: E/AndroidRuntime(630):  at java.lang.Class.getConstructor(Class.java:477)
11-20 19:14:56.446: E/AndroidRuntime(630):  at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-20 19:14:56.446: E/AndroidRuntime(630):  ... 21 more
4

3 回答 3

2

您需要 xml 中的实际类名:

<com.example.connectfour.view.ConnectFourView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>
于 2012-11-20T19:04:22.620 回答
1

您的 XML 中的包名称错误。它不应该com.example...只是example...

于 2012-11-20T18:57:43.543 回答
0

发现了答案。我在 ConnectFourView.java 中缺少以下代码

public ConnectFourView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setMinimumWidth(100);
    setMinimumHeight(20);
}

感谢所有帮助我发现这一点的人

于 2012-11-20T19:31:22.373 回答