Android 膨胀 xml 布局生成 RuntimeException

发布于 2024-09-26 00:31:18 字数 1389 浏览 7 评论 0原文

我是 Android 新手,我正在尝试在 xml 中扩充布局,但出现 RuntimeException。除了我的活动类和扩展 SurfaceView 的类之外,我几乎删除了所有内容。 谁能告诉我我做错了什么?

main.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>

Rita.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);
  }
}

Panel.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);
  }          
} 

I'm new to Android and i'm trying to inflate a layout in xml but i get a RuntimeException. I have cut out almost everything except for my activity class and the class extending SurfaceView.
Can anyone tell me what i'm doing wrong?

main.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>

Rita.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);
  }
}

Panel.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);
  }          
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-10-03 00:31:18

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

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

2)添加构造函数

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

您可能想尝试一下

In order to make your code run I had to do the following:

1) change "match_parent" to "fill_parent"

2) add constructor

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

You may want to try that

白馒头 2024-10-03 00:31:18

当您报告异常时,您应该始终发布堆栈跟踪。 (在命令行运行adb logcat,或者在eclipse中查看logcat窗口)。

如果没有这个,我最好的猜测是它应该是 fill_parent,而不是 match_parent。

You should always post a stack trace when you report an exception. (Run adb logcat on the command line, or view the logcat window in eclipse).

Without that, my best guess is that it should be fill_parent, not match_parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文