I'm trying to add a custom ImageView to my main.xml, but if I start the program it closes with a forced close.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<test.testpkg.CustomImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="#drawable/bg"/>
</LinearLayout>
Java:
package test.testpkg;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
Also if I start the program in Debugger after the FC I only get this:
link text
Debugger is useless if you haven't attached the source code of Android. Moreover... it's more useful to provide the logcat output. Anyway, I think you are missing one of the constructors. Try this:
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// rest of your code....
Related
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>
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>
It's hard to say the exact reason why it doesn't work. I see at least two reasons.
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"/>
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);
}
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
... />
I was trying to make an app with a custom view, and i kept getting "error inflating class".
It must be that I am missing some of the basics when it comes to custom views, but I am not sure what. Here is a very simple program with a custom view, what more is needed to make it work?
(Notes: For the sake of this question, I put SurfaceView class inside of the Activity Class. This was not the situation in the larger application. I do not show the AndroidManifest.xml file here, but it is just what was generated by the wizard in eclipse.)
Here is the java:
package com.mypackage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
public class SimpleCustomViewActivity extends Activity {
class TheView extends SurfaceView{
private static final String TAG = "TheView";
public TheView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG,"TheView(" + context + "," + attrs + ")");
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
TheView v = (TheView) findViewById(R.id.myview);
}
}
Here is file res/layout/simple_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
When u call your own surfaceView class from the xml file u need to add the following public surfaceView creating methods:
public GameView(Context context) {
super(context);
init(context);
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
If u are using the function setContentView(gv) you only need the first one.
in xml it should be:
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.mypackage.SimpleCustomView.TheView>
I believe something like this may work though I haven't tested it:
<View
class="com.mypackage.SimpleCustomView$TheView"
id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
: declare two methods and it should be public!!
public TheView(Context context)
public TheView(Context context, AttributeSet attrs)
I want to extend the functionality of the ImageButton class, in a new class I choose to call "DialButton". To start off, I simpy extend ImageButton, and added nothing new. In my mind, this should be identical to the normal ImageButton class.
package com.com.com;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;
public class DialButton extends ImageButton{
public DialButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public DialButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DialButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
}
I insert a DialButton in XML (dont worry about the rest of the file, its irrelevant)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow
android:layout_weight="1">
<DialButton
android:id="#+id/button1"
android:background="#drawable/dialpad"
android:layout_weight="1"/>
And use the XML in my activity:
package com.com.com;
import android.app.Activity;
import android.os.Bundle;
public class Android3 extends Activity {
DialButton button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintable);
}
}
Everything compiles and installs in the emulator but when the app starts it force closes on me. If I change the XML component from a DialButton to an ImageButton, everything works fine. Why is this? What is the difference between the ImageButton class and my DialButton class that causes the DialButton component to crash the app?
In your layout file, you have to provide a 'fully-qualified' name for your custom widgets as follows...
<com.mycompany.myapp.DialButton
android:id="#+id/button1"
android:background="#drawable/dialpad"
android:layout_weight="1"/>
I created a custom imageview. But I get an InflateException when I try to run this. Can someone help me solve this?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/herinnering_background">
<be.test.ArrowImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/kompas_arrow_car"
/>
</FrameLayout>
package be.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;
public class ArrowImageView extends ImageView{
public ArrowImageView(Context context) {
super(context);
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG);
paint.setColor(Color.GREEN);
paint.setTextSize(12.0F);
canvas.drawText("Hello World in custom view", 100, 100, paint);
}
}
I think the problem is that you need to implement a constructor with the AttributeSet because this is the one used by the LayoutInflator:
ImageView(Context context, AttributeSet attrs)
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);
}
}
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
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.