Search code examples
androidseekbar

Custom SeekBar failed to instantiate - java.lang.NullPointerException


When I try to use a custom seekbar in main.xml I get failed to instantiate error message and while it is run it gives a Error inflating class message.

customseekbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/leftText"
        android:textSize="10dp"
        android:gravity="left" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView 
        android:id="@+id/centerText"
        android:textSize="20dp"
        android:gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView 
        android:id="@+id/rightText"
        android:textSize="10dp"
        android:gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <SeekBar 
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"            />

</TableRow>

CustomSeekBar.java

    package kirbz.Component.CustomSeekBar;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.SeekBar;
    import android.widget.TextView;

public class CustomSeekBar extends SeekBar {

private SeekBar mSeekBar = (SeekBar) findViewById(R.id.seekBar);
private TextView mMinText = (TextView) findViewById(R.id.leftText);
private TextView mMaxText = (TextView) findViewById(R.id.rightText);
private TextView mValueText = (TextView) findViewById(R.id.centerText);

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
     mSeekBar=(SeekBar) findViewById(R.id.seekBar);
     mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mValueText.setText(String.valueOf(new Integer(progress)));        
            }

            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                  mValueText.setText(String.valueOf(new Integer(mSeekBar.getProgress())));      
            }

            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                  mValueText.setText(String.valueOf(new Integer(mSeekBar.getProgress())));      
            }
     });
    // TODO Auto-generated constructor stub
}

public void setValues(int max, int min, int value) {
    mSeekBar.setMax(max-min);
    mMaxText.setText(String.valueOf(max));
    mMinText.setText(String.valueOf(min));
    mSeekBar.setProgress(value - min);
    mValueText.setText(String.valueOf(value));

}

}

My main.xml is

    <?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" >

<kirbz.Component.CustomSeekBar.CustomSeekBar
    android:id="@+id/customSeekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_gravity="left" />

LogCAT

05-01 17:39:51.227: E/AndroidRuntime(675): FATAL EXCEPTION: main
05-01 17:39:51.227: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{kirbz.Component.CustomSeekBar/kirbz.Component.CustomSeekBar.CustomSeekBarActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class kirbz.Component.CustomSeekBar.CustomSeekBar
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.os.Looper.loop(Looper.java:123)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 17:39:51.227: E/AndroidRuntime(675):  at java.lang.reflect.Method.invokeNative(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675):  at java.lang.reflect.Method.invoke(Method.java:507)
05-01 17:39:51.227: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 17:39:51.227: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 17:39:51.227: E/AndroidRuntime(675):  at dalvik.system.NativeStart.main(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class kirbz.Component.CustomSeekBar.CustomSeekBar
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-01 17:39:51.227: E/AndroidRuntime(675):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.Activity.setContentView(Activity.java:1657)
05-01 17:39:51.227: E/AndroidRuntime(675):  at kirbz.Component.CustomSeekBar.CustomSeekBarActivity.onCreate(CustomSeekBarActivity.java:11)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 17:39:51.227: E/AndroidRuntime(675):  ... 11 more
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: java.lang.reflect.InvocationTargetException
05-01 17:39:51.227: E/AndroidRuntime(675):  at java.lang.reflect.Constructor.constructNative(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-01 17:39:51.227: E/AndroidRuntime(675):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-01 17:39:51.227: E/AndroidRuntime(675):  ... 21 more
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: java.lang.NullPointerException
05-01 17:39:51.227: E/AndroidRuntime(675):  at kirbz.Component.CustomSeekBar.CustomSeekBar.<init>(CustomSeekBar.java:18)
05-01 17:39:51.227: E/AndroidRuntime(675):  ... 24 more

Could you please advise what am I missing??

Appreciate your time and thanks in advance.


Solution

  • I changed the entire approach and managed to achieve what I wanted with the following -

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TableRow 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView 
                android:id="@+id/leftText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:gravity="left" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:paddingLeft="10dp"/>
    
            <TextView 
                android:id="@+id/centerText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:gravity="center" android:layout_weight="1"/>
    
            <TextView 
                android:id="@+id/rightText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:gravity="right" android:layout_weight="1" android:paddingRight="10dp"/>
        </TableRow>
    
    
        <TableRow 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <SeekBar 
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
     />
    
        </TableRow>
    </LinearLayout>
    

    The CustomBar.java is the custom class that inflates the layout as a LAYOUT

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.widget.LinearLayout;
    import android.widget.SeekBar;
    import android.widget.Toast;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.view.View;
    import android.widget.TextView;
    
    public class CustomBar extends LinearLayout {
    
        private SeekBar mSeekBar;
        private TextView mMinText;
        private TextView mMaxText;
        private TextView mValueText;
    
        public CustomBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.customseekbarlayout, this);
    
            initializeLayout();
    
            // TODO Auto-generated constructor stub
    
        }
    
        public void setValues(int max, int min, int value) {
            mSeekBar.setMax(max-min);
            mMaxText.setText(String.valueOf(max));
            mMinText.setText(String.valueOf(min));
            mSeekBar.setProgress(value - min);
            mValueText.setText(String.valueOf(value));
    
        }
    
        private void initializeLayout() {
            this.mSeekBar = (SeekBar) findViewById(R.id.seekBar);
            this.mMinText = (TextView) findViewById(R.id.leftText);
            this.mMaxText = (TextView) findViewById(R.id.rightText);
            this.mValueText = (TextView) findViewById (R.id.centerText);
    
            this.mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    // TODO Auto-generated method stub
                    mValueText.setText(String.valueOf(progress));
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(getContext(), "Test2", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(getContext(), "Test3", Toast.LENGTH_SHORT).show();
                }
    
            });
        }
    
        public int getValue(){
            return mSeekBar.getProgress();
        }
    
    
    }
    

    The main.xml is

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <kirbz.Components.CustomSeekBar.CustomBar
            android:id="@+id/customBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </kirbz.Components.CustomSeekBar.CustomBar>
    
    </LinearLayout>
    

    and the MainActivity is

    import android.app.Activity;
    import android.os.Bundle;
    
        public class CustomSeekBarActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final CustomBar mSeekBar = (CustomBar) findViewById(R.id.customBar1);
    
    
            mSeekBar.setValues(8,4,6);
        }
    }