why this exception: java.lang.RuntimeException: Error inflating class - android

I am trying to develop a tiny android app that just opens a browser to a website. I am using mac OS 10.6 and the latest android/eclipse tools. But I keep getting this exception:
11-29 13:03:55.113: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycomp/com.mycomp.pack}: android.view.InflateException: Binary XML file line #7: Error inflating class com.mycomp.MyTextView
Here is my code:
package com.mycomp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class myapp extends Activity {
/** Called when the activity is first created. */`
private MyTextView myview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("onCreate", "onCreate ");
myview = (MyTextView)findViewById(R.id.myview);
myview.setParent(this);
}
public static class MyTextView extends TextView {
private View mLastVisChangedView;
private int mLastChangedVisibility;
private Activity parent;
public void setParent(Activity p){
parent =p;
}
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View getLastVisChangedView() {
return mLastVisChangedView;
}
public int getLastChangedVisibility() {
return mLastChangedVisibility;
}
#Override
protected void onVisibilityChanged(View changedView, int visibility){
Log.d("onVisibilityChanged", "new vis == " + visibility);
if (parent !=null && visibility == View.VISIBLE){
parent.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mysite.com")));
parent.moveTaskToBack(true);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.mycomp.MyTextView
android:id="#+id/myview"
android:text="#string/hello"/>
</LinearLayout>

The type would be com.mycomp.myapp.MyTextView. (Or $ since it's an inner class?)
You may also need to specify the view as follows:
<view class="com.mycomp.myapp$MyEditText" ...
I don't think you need an onFinishInflate in this case.

You should put the MyTextView class in a separate file. Then com.mycomp.MyTextView would be correct.

Related

CustomView in android throws inflate exception

I have to create a custom view in my android application, for that I have wrote code as follows
xml
<com.package.custom.CustomView
android:id="#+id/my_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
CustomView.java
package com.package.custom;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class CustomView extends View {
Paint customPaint;
public CustomView(Context context) {
super(context);
customPaint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
customPaint.setStyle(Paint.Style.FILL);
customPaint.setAntiAlias(true);
customPaint.setColor(getResources().getColor(android.R.color.holo_blue_dark));
canvas.drawPaint(customPaint);
}
}
Activity
public class CustomViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_button);
CustomView customView = (CustomView) findViewById(R.id.my_view);
}
}
but while I am running the application I am getting error ::
3178-3178/com.package.custom E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.custom/com.package.custom.CustomViewActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.package.custom.CustomView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
What is the reason ? is there any problem in my code ? how do I resolve this ?
Try to change:
public class CustomView extends TextView {
Paint customPaint;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
customPaint = new Paint();
}
Name of the class constructor are not the same.
View class does not have method android:text="" change base class to TextView

Unable to display the created CustomView

Recently I've been trying to create a CustomView.
I am following the tutorial and did as directed but when i tried to run the code my CustomView was not displayed on my android screen.
My code for attrs.xml is:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TimeView">
<attr name="text" format="string"/>
<attr name="setColor" format="boolean"/>
</declare-styleable>
</resources>
Here is the code for my CustomView i.e TimeView.java:-
package com.example.custom;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
public class TimeView
extends TextView{
public String titleText;
public boolean color;
public TimeView(Context context) {
super(context);
setTimeView();
// TODO Auto-generated constructor stub
}
public TimeView(Context c,AttributeSet as)
{
super(c,as);
TypedArray ty=c.obtainStyledAttributes(as,R.styleable.TimeView);
int count=ty.getIndexCount();
try{
for(int i=0;i<count;i++)
{
int attr=ty.getIndex(i);
if(attr==R.styleable.TimeView_text)
{
titleText=ty.getString(attr);
}
else if(attr==R.styleable.TimeView_setColor)
{
color=ty.getBoolean(attr, false);
decorate();
}
}
}
finally
{
ty.recycle();
}
}
public TimeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTimeView();
}
public void setTimeView()
{
SimpleDateFormat sdf=new SimpleDateFormat("hh.mm aa");
String time=sdf.format(Calendar.getInstance().getTime());
if(this.titleText!=null)
{
setText(this.titleText+" "+time);
}
else
setText(time);
}
public void decorate()
{
if(this.color==true)
{
setShadowLayer(4, 2, 2, Color.rgb(250, 00, 250));
setBackgroundColor(Color.CYAN);
}
else{
setBackgroundColor(Color.RED);
}
}
}
and lastly here is the code for my activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.custom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.custom.MainActivity" >
<com.example.custom.TimeView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
custom:text="My View"
custom:setColor="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
I am getting this result:-
I don't know where i am doing mistake.
Please help me!
Thank you in advance.
Just overwrite your code with my code it's working. You just make mistake while retrieving attributes.
Don't forget to add your package name at first line
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
public class TimeView
extends TextView {
public String titleText;
public boolean color;
public TimeView(Context context) {
super(context);
setTimeView(context, null);
}
public TimeView(Context c, AttributeSet as) {
super(c, as);
setTimeView(c, as);
}
public TimeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTimeView(context, attrs);
}
public void setTimeView(Context c, AttributeSet attrs) {
TypedArray a;
if (attrs != null) {
a = c.getTheme().obtainStyledAttributes(
attrs,
R.styleable.BLProgress,
0, 0);
} else {
throw new IllegalArgumentException("Must have to pass the attributes");
}
try {
titleText = a.getString(R.styleable.TimeView_text);
color = a.getBoolean(R.styleable.TimeView_setColor, false);
} finally {
a.recycle();
}
SimpleDateFormat sdf = new SimpleDateFormat("hh.mm aa");
String time = sdf.format(Calendar.getInstance().getTime());
if (this.titleText != null) {
setText(this.titleText + " " + time);
} else
setText(time);
decorate();
}
public void decorate() {
if (color) {
setShadowLayer(4, 2, 2, Color.rgb(250, 00, 250));
setBackgroundColor(Color.CYAN);
} else {
setBackgroundColor(Color.RED);
}
}
}
here is the screen shot ..

I would like to know how to draw using canvas over my XML layout

I've created a custom XML layout and I’m trying to draw the map background and the pawn player( the bitmap) over it.
Instead, its painting the pawn player over a white background without the map background that I put as the background on the XML file.
MyViev Class:
package com.example.alpha;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Bitmap playerW;
float changingY;
float changingX;
public MyView(Context context) {
super(context);
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.black);
changingY=0;
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW,4+changingX, (canvas.getHeight())-288-changingY, null);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
my MainActivity Class:
package com.example.alpha;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new MyView(this);
setContentView(ourView);
}
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<view class="com.example.alpha.MyView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mapeasy"
/>
It's because you're not actually setting it to use your XML layout. Instead, you've set the content View to be a new instance of MyView, which doesn't have the XML background property set.
Therefor you have 2 options:
Option 1: Call ourView.setBackgroundDrawable(R.drawable.mapeasy); after you created a new instance of MyView.
or
Option 2: Set the content View to be your actual layout file and then find your MyView by using findViewById(int).
Eg.
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
ourView = (MyView)findViewById(R.id.myView);
}
}

Error inflating custom Switch class: Android

I have created a custom switch and I am using that in a fragment. But i am getting the following error:
12-30 17:46:55.456: E/AndroidRuntime(13351): android.view.InflateException: Binary XML file line #47: Error inflating class com.arrayent.arrayentcesdemo.customview.CustomSwitch
12-30 17:46:55.456: E/AndroidRuntime(13351): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.Att
The custom switch that i am using is:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.Switch;
public class CustomSwitch extends Switch {
public CustomSwitch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private CompoundButton.OnCheckedChangeListener myListener = null;
#Override
public void setOnCheckedChangeListener(
CompoundButton.OnCheckedChangeListener listener) {
if (this.myListener == null)
this.myListener = listener;
super.setOnCheckedChangeListener(listener);
}
public void silentlySetChecked(boolean checked) {
toggleListener(false);
super.setChecked(checked);
toggleListener(true);
}
private void toggleListener(boolean on) {
if (on) {
this.setOnCheckedChangeListener(myListener);
} else
this.setOnCheckedChangeListener(null);
}
}
And the switch in the layout file is;
<com.arrayent.arrayentcesdemo.customview.CustomSwitch
android:id="#+id/light_switch"
android:layout_width="100dip"
android:layout_height="5dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dip"
android:scaleY="0.7"
android:textOff=" "
android:textOn=" "
android:thumb="#drawable/plug_thumb_bitmap"
android:track="#drawable/btntoggle_selector_light" />
Could someone please help me figure this out?
Thanks
You need to implement a constructor that takes in a Context and AttributeSet. You can delegate the implementation to the 3-arg constructor:
public CustomSwitch(Context context, AttributeSet attrs) {
this(context, attrs, null);
}
The <init> in the stack trace refers to object initialization, i.e. constructor.

TextView not effected onProgressChanged of seekBar

I have this extension of SeekBar:
package com.simplemathgame;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekBarPlus extends SeekBar implements OnSeekBarChangeListener{
private TextView numberOfDrills;
public SeekBarPlus(Context context) {
super(context);
}
public SeekBarPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SeekBarPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
Log.w("SeekBarChanged", "change to" + progress);
numberOfDrills.setText(String.valueOf(progress));
}
public void setTextView(TextView textView){
numberOfDrills = textView;
Log.w("SeekBar", "text to bar");
}
}
And here is the main activity code:
package com.simplemathgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.simplemathgame.SeekBarPlus;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBarPlus addSeekBar = (SeekBarPlus) findViewById(R.id.add_seek_bar);
SeekBarPlus subSeekBar = (SeekBarPlus) findViewById(R.id.sub_seek_bar);
SeekBarPlus mulSeekBar = (SeekBarPlus) findViewById(R.id.mul_seek_bar);
SeekBarPlus divSeekBar = (SeekBarPlus) findViewById(R.id.div_seek_bar);
TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
addSeekBar.setTextView(numberOfAddDrills);
subSeekBar.setTextView(numberOfSubDrills);
mulSeekBar.setTextView(numberOfMulDrills);
divSeekBar.setTextView(numberOfDivDrills);
}
}
After I move the progress bar nothing happens, I have all the needed elements (TextViews).
I would like:
I would like the SeekBarPlus Automatically listen to it's changes and react as I have coded in the onProgressChanged method,in other words I would like that onProgressChanged would be triggered without any code in the main activity.
Screenshot
After reading the documentation:
Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to
be notified of the user's actions.
You need to have a listener on your Seekbar in order to update.
Update
This is possible, but you have to create a single view to house everything and then attach it it that way. Once you have created this, then add the custom view to your layout file. Then add callbacks as necessary from your custom view. Of course this implies that you added your listeners IN your custom view class.
Example
You would have something like this in your class, make sure to set the orientation of the views:
package com.blah.my.package
class MyCustomClass extends LinearLayout{
CustomSeekbar v1 ...
CustomSeekbar v2 ...
CustomSeekbar v3 ...
CustomSeekbar v4 ...
CustomSeekbar v5 ...
CustomSeekbar v6 ...
...
Constructors and methods n' stuff...
}
Once you have this, then in your layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.blah.my.package.MyCustomClass ... />
...
<LinearLayout>
To learn more:
http://developer.android.com/training/custom-views/index.html
The easiest way is to implement OnSeekBarChangeListener directly on your extended class:
public class SeekBarPlus extends SeekBar implements OnSeekBarChangeListener{
public SeekBarPlus(Context context) {
super(context);
setOnSeekBarChangeListener(this);
}
public SeekBarPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setOnSeekBarChangeListener(this);
}
public SeekBarPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnSeekBarChangeListener(this);
}
....
}
Otherwise none of the on* methods will get called. Note: this really should be a composite widget and you shouldn't need to be passing in the TextView.
You also need to do what Sergio suggested and not use the int value directly: numberOfDrills.setText("" + progress);
That's because you're sending an int to setText(), so it doesn't work because it expects a String. Change the line to:
numberOfDrills.setText("" + progress);
Or:
numberOfDrills.setText(String.valueOf(progress));
You may want to change this 4 lines too in your MainActivity, you are setting 4 TextViews to the same seekbar:
/*addSeekBar.setTextView(numberOfAddDrills);
addSeekBar.setTextView(numberOfSubDrills);
addSeekBar.setTextView(numberOfMulDrills);
addSeekBar.setTextView(numberOfDivDrills);*/
addSeekBar.setTextView(numberOfAddDrills);
subSeekBar.setTextView(numberOfSubDrills);
mulSeekBar.setTextView(numberOfMulDrills);
divSeekBar.setTextView(numberOfDivDrills);

Categories

Resources