Trying to create custom textview but showing error - android

Here is my code for class test.java
package com.example.testdroid;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.widget.TextView;
public class test extends TextView{
public test(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setText("Hello World");
this.setTextSize(20f);
this.setTextColor(Color.RED);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
main.xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.testdroid.test
android:id="#+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/button1"
android:layout_marginBottom="68dp"
/>
</RelativeLayout>
Logcat :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdroid/com.example.testdroid.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.example.testdroid.test

Create a class for textview like as follow:-
public class TextViews extends TextView{
public TextViews (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TextViews (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextViews (Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
this.setText("Hello World");
this.setTextSize(20f);
this.setTextColor(Color.RED);
}
}
}

Related

compound layout doesn't show inner views

I extended from LinearLayout and inflated views, but on the screen those views in my layout are not appeared. I see my view group in editor, but it seems like it's empty. No views inside it.
FragmentPickerViewGroup looks like this:
package com.test.pecode.myapplication;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class FragmentPickerViewGroup extends LinearLayout {
private ImageView background;
private Button minusButton;
private Button plusButton;
public FragmentPickerViewGroup(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.HORIZONTAL);
inflate(getContext(), R.layout.view_fragment_picker, this);
minusButton = findViewById(R.id.minus_btn);
plusButton = findViewById(R.id.plus_btn);
background = findViewById(R.id.background_rectangle);
minusButton.setVisibility(INVISIBLE);
minusButton.setGravity(Gravity.TOP | Gravity.START);
minusButton.setBackgroundResource(R.mipmap.button_bg);
background.setBackgroundResource(R.mipmap.rectangle);
background.setBackgroundResource(R.mipmap.rectangle);
plusButton.setBackgroundResource(R.mipmap.button_bg);
plusButton.setGravity(Gravity.TOP | Gravity.END);
}
public FragmentPickerViewGroup(Context context, #Nullable AttributeSet
attrs) {
super(context, attrs);
init();
}
public FragmentPickerViewGroup(Context context, #Nullable AttributeSet
attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public FragmentPickerViewGroup(Context context, AttributeSet attrs, int
defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
}
I used merge tag for my layout. Probably something goes wrong there? This is my layout, which I inflate into my view group:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/minus_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/background_rectangle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:contentDescription="#string/background_rectangle" />
<Button
android:id="#+id/plus_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</merge>

Error: android.view.inflateExeption in CustomTextView

I have a menu activity that gets user setting of font and size and line spacing of text in all of TextView's, so i need a CustomTextView class to set these changes but my custom TextView does't work and give inflate Exception in logcat window. tank's a lot for your helps!
my CustomTextView:
package com.niloo.test2;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
Context context;
SharedPreferences shp = context.getSharedPreferences("text", Context.MODE_PRIVATE);
int size = shp.getInt("size", 22);
int fasle = shp.getInt("fasle", 1);
String font = shp.getString("font", "bmitra");
public CustomTextView(Context context) {
super(context);
if (!isInEditMode())
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
setTextSize(size);
setLineSpacing(fasle, 1);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (! isInEditMode())
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
setTextSize(size);
setLineSpacing(fasle, 1);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode())
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
setTextSize(size);
setLineSpacing(fasle, 1);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
my XML code:
<com.niloo.test2.CustomTextView
android:id="#+id/tv_onv_amoozsh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp"
android:layout_weight="0.5"
android:textColor="#color/sabz_lajani"
android:textSize="25sp"
android:textStyle="bold" />
<com.niloo.test2.CustomTextView
android:id="#+id/tv_num_amoozsh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="2dp"
android:layout_weight="0.75"
android:textColor="#color/sabz_lajani"
android:textSize="35sp" />
and my main class:
final CustomTextView tv_onv_amoozsh= (CustomTextView) findViewById(R.id.tv_onv_amoozsh);
tv_onv_amoozsh.setText("page");
final CustomTextView tv_num_amoozsh=(CustomTextView) findViewById(R.id.tv_num_amoozsh);
tv_num_amoozsh.setText(sfhe_num);
LogCat Error:
02-03 22:41:38.329: E/AndroidRuntime(19952): FATAL EXCEPTION: main
02-03 22:41:38.329: E/AndroidRuntime(19952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.niloo.test2/com.niloo.test2.MainActivity}: android.view.InflateException: Binary XML file line #143: Error inflating class com.niloo.test2.CustomTextView
The problem is most likely that you're initializing shp, size, fasle, and font outside of the constructor. These initializations happen before any constructor code is run. context is never set and is null when getSharedPreferences is called on it.
Try this:
public class CustomTextView extends TextView {
private SharedPreferences shp;
private int size;
private int fasle;
private String font;
public CustomTextView(Context context) {
super(context);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
if (isInEditMode()) {
return;
}
shp = context.getSharedPreferences("text", Context.MODE_PRIVATE);
size = shp.getInt("size", 22);
fasle = shp.getInt("fasle", 1);
font = shp.getString("font", "bmitra");
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/" + font));
setTextSize(size);
setLineSpacing(fasle, 1);
}
}
Do you have this at the first view in your layout?
xmlns:custom="http://schemas.android.com/apk/res-auto"
Please post the full layout file (reduce it in your project to the minimum that is still failing. Maybe only have one view in it - your custom text )
EDIT:
The xml code you posted is invalid. Here is the fixed version that should work (do not omit "http://", remove semicolons)
<?xml version="1.0" encoding="utf-8"?>
<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-auto"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/sabzabi_kmrng"
android:gravity="top"
android:orientation="vertical"
tools:context="com.niloo.test2.MainActivity">
</LinearLayout>
The error is coming from this line:
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
Here we don't need to mention the keyword "fonts/" for path, because getAssets() method giving the route path of assets folder. So you can directly give font name like below:
setTypeface(Typeface.createFromAsset(context.getAssets(), font));

I have a subclass of ImageView, but when I add this into XML it gives inflate error

This seems like a simple thing, but it does not run. What am I doing wrong?
I'm subclassing ImageView, but if I put it inside XML layouts, it gives:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myappSubclass/com.example.myappSubclass.MyActivity}:
android.view.InflateException: Binary XML file line #21: Error
inflating class com.example.myappSubclass.ImageViewSubclass
Here is my code:
package com.example.myappSubclass;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.util.jar.Attributes;
/**
* Created by me on 2014-07-11.
*/
public class ImageViewSubclass extends ImageView {
public Context thisContext;
public ImageViewSubclass(Context context, AttributeSet attrs, Context thisContext) {
super(context, attrs);
this.thisContext = thisContext;
}
public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle, Context thisContext) {
super(context, attrs, defStyle);
this.thisContext = thisContext;
}
public ImageViewSubclass(Context context) {
super(context);
this.thisContext = context;
}
public void changeImage(){
this.setImageLevel(0);
}
And inside the XML I have it simply:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
<Button android:layout_width="match_parent" android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="Test"
/>
<com.example.myappSubclass.ImageViewSubclass android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!--<ImageView android:layout_width="fill_parent" android:layout_height="fill_parent"/>-->
</LinearLayout>
Custom views must have predefined constructors to be able to inflate them from an XML layout. You should remove thisContext from all of them.
public ImageViewSubclass(Context context) {
super(context);
...
}
public ImageViewSubclass(Context context, AttributeSet attrs) {
super(context, attrs);
...
}
public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
...
}
Why did you add that extra parameter anyway? All views have a getContext() method if that's what you needed.

SurfaceView failed to instantiate

I'm trying to implement a custom SurfaceView in my activity's XML file, and then reference it within the activity's code for drawing etc. However, I'm getting the following error:
The following classes could not be instantiated:
- com.example.animateddrawable.MainActivity.DrawingPanel
MainActivity Code:
package com.example.animateddrawable;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
...
com.example.animateddrawable.MainActivity.DrawingPanel.PanelThread;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dp = new DrawingPanel(this, null);
setContentView(R.layout.activity_main);
}
public class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
public DrawingPanel( Context context, AttributeSet attributeSet){
super(context, attributeSet);
init();
}
public void init(){
getHolder().addCallback(this);
}
}
}
XML Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.animateddrawable.MainActivity.DrawingPanel
android:id="#+id/surfaceView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</LinearLayout>
Any ideas?
try to add a constructor like this:
public DrawingPanel(Context context) {
super(context);
getHolder().addCallback(this);
}
public DrawingPanel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getHolder().addCallback(this);
}
add this to your other constructer as well:
getHolder().addCallback(this);

Inflating a custom control in Android

In my activity I have a custom made control. In onCreate I inflate the activity's view as normal with:
setContentView(R.layout.image_viewer);
Here is my 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"
android:orientation="vertical" >
<MyCustomImageView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And here is a reduced piece of code for MyCustomTextView:
public class MyCustomImageView extends ImageView
{
Context context;
public MyCustomImageView(Context context)
{
super(context);
sharedConstructing(context);
}
public MyCustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
sharedConstructing(context);
}
}
The exception that gets generated reads:
android.view.InflateException: Binary XML file line #7: Error inflating class MyCustomImageView
you need to add the package name:
<com.my.package.MyCustomTextView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You have to create constructors calling the superclass constructors, because they will be called by the inflater:
public class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
And you have to put the full package name in your XML:
<com.mypackagename.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Categories

Resources