草庐IT

Android inflating xml布局产生RuntimeException

coder 2024-07-02 原文

我是 Android 的新手,我试图在 xml 中扩展布局,但我得到了一个 RuntimeException。除了我的 Activity 类和扩展 SurfaceView 的类之外,我几乎删除了所有内容。 谁能告诉我我做错了什么?

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:layout_height="match_parent">    
<com.hj.Panel    
android:id="@+id/SurfaceView01"      
android:layout_width="match_parent"      
android:layout_height="match_parent"/>    
</FrameLayout>

丽塔.java:

package com.hj;

import android.app.Activity;
import android.os.Bundle;

public class Rita extends Activity {
/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

面板.java:

package com.hj;

import android.content.Context; 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceView;

class Panel extends SurfaceView {    
  private Paint mPaint;

  public Panel(Context context) {
    super(context); 
  }  
  @Override
  public void onDraw(Canvas canvas) {          
    mPaint = new Paint();  
    canvas.drawRect(0, 0, 322, 644, mPaint);
  }          
} 

最佳答案

为了让您的代码运行,我必须执行以下操作:

1) 将“match_parent”更改为“fill_parent”

2) 添加构造函数

  public Panel(Context context, AttributeSet atts) {
    super(context, atts); 
  } 

你可能想试试看

关于Android inflating xml布局产生RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3846993/

有关Android inflating xml布局产生RuntimeException的更多相关文章

  1. ruby - nanoc 和多种布局 - 2

    是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'

  2. ruby - 猴子修补 float 中缀运算符产生意想不到的结果 - 2

    重新定义Float#/似乎没有效果:classFloatdef/(other)"magic!"endendputs10.0/2.0#=>5.0但是当另一个中缀运算符Float#*被重新定义时,Float#/突然采用了新的定义:classFloatdef/(other)"magic!"enddef*(other)"spooky"endendputs10.0/2.0#=>"magic!"我很想知道是否有人可以解释这种行为的来源,以及其他人是否得到相同的结果。ruby:ruby2.0.0p353(2013-11-22)[x64-mingw32]要快速确认错误,请运行thisscript.

  3. ruby - 为什么 Gemfile 语义版本控制运算符 (~>) 会产生与一个数字不一致的结果? - 2

    gemspec语义版本控制运算符~>(又名twiddle-wakka,又名pessimistic运算符)允许限制gem版本但允许进行一些升级。我经常看到它可以读作:"~>3.1"=>"Anyversion3.x,butatleast3.1""~>3.1.1"=>"Anyversion3.1.x,butatleast3.1.1"但是有了一个数字,这条规则就失效了:"~>3"=>"Anyversionx,butatleast3"*NOTTRUE!*"~>3"=>"Anyversion3.x"*True.Butwhy?*如果我想要“任何版本3.x”,我可以只使用“~>3.0”,这是一致的。就

  4. ruby - 为什么 openssl 在 windows 上产生错误但在 centos 上不产生错误:PKCS12_parse: mac verify failure (OpenSSL::PKCS12::PKCS12Error) - 2

    require'openssl'ifARGV.length==2pkcs12=OpenSSL::PKCS12.new(File.read(ARGV[0]),ARGV[1])ppkcs12.certificateelseputs"Usage:load_cert.rb"end运行它会在Windows上产生错误,但在Linux上不会。错误:OpenSSL::PKCS12::PKCS12Error:PKCS12_parse:macverifyfailurefrom(irb):21:ininitializefrom(irb):21:innewfrom(irb):21fromC:/Ruby192/

  5. ruby-on-rails - 我可以在没有 Controller 的情况下直接从 routes.rb 渲染布局吗? - 2

    我想为网站的管理和公共(public)部分设置一对样式指南。每个都需要自己的布局,其中包含静态html和调用erbpartials的混合(因此静态页面不会削减它)。我不需要Controller来为这些页面提供服务,而且我不希望有效的仅开发内容使其余代码困惑。这让我想知道是否有一种方法可以直接呈现布局。免责声明:我明白这不是我应该经常/永远做的事情,而且我知道有很多争论可以解释为什么这是一个坏主意。我对这是否可能感兴趣。有没有办法让我直接从routes.rb渲染布局而不通过Controller? 最佳答案 出于某种奇怪的原因,我想暂时

  6. ruby-on-rails - 设计 Controller 如何改变布局? - 2

    这个问题在这里已经有了答案:differentlayoutforsign_inactionindevise(8个答案)关闭7年前。如何更改设计Controller中的布局?

  7. Ruby 要求 'tk' 产生 LoadError : no such file to load -- tk - 2

    我无法让ruby​​成功地require'tk'。我正在使用rvm、ruby2.0.0、ActiveTcl-8.6和Ubuntu12.04LTS。我已经运行了随ActiveTcl一起提供的wish,它似乎正在运行。我查看了RVM网站http://rvm.io/integration/tk和几个像这样的StackOverflow问题RVMRubywithTKinstallation(OSX).我在不同版本的ruby​​上多次尝试rvmreinstall2.0.0--enable-shared--enable-pthread--with-tk--with-tcl但没有成功。有什么想法吗?当

  8. ruby - 将字符串插入数组中的数组会产生意外结果 - 2

    我在Ruby中有一个包含5个空数组的数组。我正在尝试使用运算符将字符串插入第一个数组,但结果是字符串被插入所有数组。请帮助我理解这一点。预期的输出是:#=>[["car"],[],[],[],[]]但我得到:#=>[["car"],["car"],["car"],["car"],["car"]]IRB转储:1.9.3-p194:001>output=Array.new(5,[])=>[[],[],[],[],[]]1.9.3-p194:002>output.inspect=>"[[],[],[],[],[]]"1.9.3-p194:003>output[0].inspect=>"[]"

  9. ruby - 在没有机架的情况下运行 Capybara 在使用 url 参数时会产生错误 - 2

    这是我的设置,基于此建议:HowtogetCucumber/Capybara/Mechanizetoworkagainstexternalnon-railssite在我将参数添加到URL之前它一直有效。对解决这个问题有什么建议吗?require'rspec'require'capybara/rspec'require'capybara/dsl'@test_url="test"RSpec.configuredo|config|config.includeCapybara::DSLendCapybara.configuredo|config|config.run_server=falsec

  10. ruby-on-rails - Rails - 如何在布局中查找域 url - 2

    我有request.env['http_host']在本地主机上工作,但在heroku的布局页面中引用时会导致错误。此请求在View中工作并显示正确的基本url,但是当我将代码移动到布局时它会导致错误。注意-我正在使用它来为html电子邮件中的图像构建绝对url。收到错误:ActionView::Template::Error(undefinedmethod`env'fornil:NilClass): 最佳答案 如果你想要没有端口的主机,只需使用:request.host编辑:糟糕,我刚刚注意到您正在使用View中的代码。我不知道它

随机推荐