Android inflates XML layout throws RuntimeException

I'm new to Android, and I'm trying to inflate the layout in xml, but I get a RuntimeException. I cut out almost everything except the activity class and the class that extends SurfaceView. Can someone tell me what I am 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);
  }          
} 
+3
source share
2 answers

To execute your code, 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 can try this

+1
source

, . ( adb logcat logcat eclipse).

, , fill_parent, match_parent.

0

Source: https://habr.com/ru/post/1767683/


All Articles