views:

84

answers:

4

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);
  }          
} 
A: 

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.

Mayra
'match_parent' doesn't seem to be the problem. This is the stack trace:
Henrik
'match_parent' doesn't seem to be the problem. This is the stack trace:
Henrik
'match_parent' doesn't seem to be the problem. This is the stack trace:
Henrik
A: 

Stack trace:

Rita [Android Application]
DalvikVM[localhost:8628]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running)

Stack trace from LogCat:

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

Henrik
You might not have been able to this time, but in the future you should edit your original post to add details, not post an answer.
Mayra
Are you sure that that is the only stack trace that shows up in logcat? I don't think what you posted is the real problem.
Mayra
Try to let it run after the execution stops at exception and then check LogCat for any additional messages
Asahi
+1  A: 

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

Asahi
A: 

Thanks for your time and effort! you are correct, the missing part was the AttributeSet.

Henrik