草庐IT

安卓.view.InflateException : Binary XML file line #7: Error inflating class

coder 2023-12-06 原文

我在尝试运行我的项目时收到错误消息。我想为 Android 创建 Tic Tac Toe,我使用下面的自定义 View 来创建 Tic Tac Toe 棋盘:

package org.me.TicTacToe.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;

public class TicTacToeBoard extends View {

    private Tile[][] tile = null;   //Abstract class to create tiles in Tic Tac Toe Board
    int boardWidth = 3; //It mean Tic Tac Toe board is consists of 3x3 tiles
    private int width;
    private int height;
    private Paint brush;

    public TicTacToeBoard(Context context) {
        super(context);

        brush = new Paint();
        this.brush.setARGB(255, 0, 0, 0);
        this.brush.setAntiAlias(true);
        this.brush.setStyle(Style.STROKE);
        this.brush.setStrokeWidth(5);

        width = this.getWidth();
        height = this.getHeight();

        initBoard();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < tile.length; i++) {
            for (int j = 0; j < tile[0].length; j++) {
                tile[i][j].draw(canvas, getResources(), j, i,
                    (this.getWidth() + 3) / tile.length,
                    this.getHeight() / tile[0].length);
            }
        }

        int xs = this.getWidth() / boardWidth;
        int ys = this.getHeight() / boardWidth;
        for (int i = 0; i <= boardWidth; i++) {
            canvas.drawLine(xs * i, 0, xs * i, this.getHeight(), brush);
        }
        for (int i = 0; i <= boardWidth; i++) {
            canvas.drawLine(0, ys * i, this.getWidth(), ys * i, brush);
        }

        super.onDraw(canvas);
    }

    public void initBoard(){
        tile = new Tile[boardWidth][boardWidth];

        int xss = width / boardWidth;
        int yss = height / boardWidth;

        for (int row = 0; row < boardWidth; row++) {
            for (int colmn = 0; colmn < boardWidth; colmn++) {
                tile[row][colmn] = new TileEmpty(xss * colmn, row * yss);
                // TileEmpty is extend class from Tile.
                // TileEmpty represent empty tile in Tic Tac Toe
            }
        }
    }
}

接下来,我将它放在基于 XML 的 Activity 布局中,如下所示:

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

    <org.me.TicTacToe.ui.TicTacToeBoard
        android:id="@+id/game1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

所以我在 Eclipse LogCat 窗口中收到致命异常错误:

AndroidRuntime(329): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.me.TicTacToe/org.me.TicTacToe.ui.TicTacToeActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class org.me.TicTacToe.ui.TicTacToeBoard

如何解决?

完整的 Logcat 行:

02-12 10:22:31.989: D/AndroidRuntime(329): Shutting down VM
02-12 10:22:31.989: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-12 10:22:32.008: E/AndroidRuntime(329): FATAL EXCEPTION: main
02-12 10:22:32.008: E/AndroidRuntime(329): java.lang.RuntimeException:
                    Unable to start activity ComponentInfo{org.me.TicTacToe/org.rme.TicTacToe.ui.TicTacToeActivity}:
                    android.view.InflateException: Binary XML file line #18: Error inflating class org.me.TicTacToe.ui.TicTacToeBoard
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 10:22:32.008: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:22:32.008: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:507)
02-12 10:22:32.008: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 10:22:32.008: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 10:22:32.008: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)
02-12 10:22:32.008: E/AndroidRuntime(329): Caused by: android.view.InflateException: Binary XML file line #7:
                    Error inflating class org.me.TicTacToe.ui.TicTacToeBoard
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.createView(LayoutInflater.java:508)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-12 10:22:32.008: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.Activity.setContentView(Activity.java:1657)
02-12 10:22:32.008: E/AndroidRuntime(329):  at org.me.TicTacToe.ui.TicTacToeActivity.onCreate(TicTacToeActivity.java:24)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 10:22:32.008: E/AndroidRuntime(329):  ... 11 more
02-12 10:22:32.008: E/AndroidRuntime(329): Caused by: java.lang.NoSuchMethodException: TicTacToeBoard(Context,AttributeSet)
02-12 10:22:32.008: E/AndroidRuntime(329):  at java.lang.Class.getMatchingConstructor(Class.java:643)
02-12 10:22:32.008: E/AndroidRuntime(329):  at java.lang.Class.getConstructor(Class.java:472)
02-12 10:22:32.008: E/AndroidRuntime(329):  at android.view.LayoutInflater.createView(LayoutInflater.java:480)
02-12 10:22:32.008: E/AndroidRuntime(329):  ... 21 more

最佳答案

您缺少 TicTacToeBoard(Context, Attributeset) 的构造函数。见this question ,例如。

编辑: 它就在您刚刚发布的 LogCat 中:

  Caused by: java.lang.NoSuchMethodException: TicTacToeBoard(Context,AttributeSet)

关于安卓.view.InflateException : Binary XML file line #7: Error inflating class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246407/

有关安卓.view.InflateException : Binary XML file line #7: Error inflating class的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  8. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  9. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

  10. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

随机推荐