0

I've been learning android for a week.i'm writing a simple program which draws a circle.but when i run it it tells me that the program has stopped. i read the code again and again but couldn't find the error. can you please help me.

package org.example.viewwithlines;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    static public class GraphicsView extends View 
    {
        Paint p;
        public GraphicsView(Context context) {
        super(context);
        p=new Paint();
        p.setColor(Color.MAGENTA);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawCircle(30, 40, 10, p);   
        }
        }
}

and this is the xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <org.example.viewwithlines.MainActivity.GraphicsView
    android:id="@+id/graphics" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</LinearLayout>
Marusia
  • 13
  • 1
  • 5
  • 1
    Please add the logcat output showing the exception – ianhanniballake Mar 08 '13 at 17:19
  • it compiles, but when i run it on the emulator it tells me that the app has stopped – Marusia Mar 08 '13 at 17:23
  • When you run the program on the emulator there should be a log that will show the exception. This log is called logcat. Can you find this log and paste what it shows here when the program crashes. – Brianjs Mar 08 '13 at 17:24
  • 03-08 17:14:33.751: E/AndroidRuntime(612): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) – Marusia Mar 08 '13 at 17:26

3 Answers3

1

It's hard to say the exact reason why it doesn't work. I see at least two reasons.

  1. Class name in the layout is wrong. Since GraphicsView is a nested class, it should be org.example.viewwithlines.MainActivity$GraphicsView

    <view
        class="org.example.viewwithlines.MainActivity$GraphicsView"
        android:id="@+id/graphics" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    
  2. You have to provide a constuctor that takes Context and AttributeSet as arguments

    public GraphicsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        p=new Paint();
        p.setColor(Color.MAGENTA);
    }
    
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
1

What are you trying to do?

For backgrounds, and general uses, you can do it simply creating a drawable resource and setting it to a square view:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="#FF0000"/>
</shape>
JPMagalhaes
  • 3,611
  • 1
  • 25
  • 26
0

When you extend a view, you need to add different constructors to it to make it work in different circumstances. When you use a View in xml, it uses the constructor with AttributeSet included.

Try adding one that looks like this:

    public GraphicsView(Context context, AttributeSet attribs) {
        super(context, attribs);
        p=new Paint();
        p.setColor(Color.MAGENTA);
    }

See this post, also, for a more detailed explanation.

Also, you're trying to reference an inner class in xml. When you do that, you have to use a $ instead of a .. The problem with this is that $ is an illegal character in xml tag names, so you have to do something like this instead:

<view class="org.example.viewwithlines.MainActivity$GraphicsView" 
    ...
    attribs here
    ... />
Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103