开发者

Android.view.InflateException problem

I've an app where my view is inflated from xml. It worked fine before i tried to add a Horizontal slidebar, this component extends progressBar. I've added the progress bar code to my src folder in the correct package and declared it in my layout file within a relative layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:gravity="fill">
    <com.tecmark.TouchView android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    <Button android:text="Button" android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <com.tecmark.HorizontalSlider android:id="@+id/slider" 
        android:indeterminateOnly="false" 
        android:progressDrawable="@android:drawable/progress_horizontal" 
        android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
        android:minHeight="30dip" android:maxHeight="30dip" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

My view's constructor includes the version that passes AttributeSet that is needed to inflate from xml.

public class TouchView extends View
{
    private File tempFile;
    private byte[] imageArray;
    private Bitmap bgr;
    private Bitmap bm;
    private Bitmap whitebm;
    private Paint pTouch;
    private int centreX = 1;
    private int centreY = 1;
    private int radius = 50;
    private HorizontalSlider slider;

    private static final String TAG = "*********TouchView";

    public TouchView(Context context)
    {
        super(context);
        // TouchView(context, null);
    }

    public TouchView(Context context, AttributeSet attr)
    {
        super(context, attr);

        Log.e(TAG, "******inside touchview constructor ");
        slider = (HorizontalSlider) this.findViewById(R.id.slider);
        Log.e(TAG, "******slider inflated*********** ");
        slider.setOnProgressChangeListener(changeListener);

        // ......get bitmap and process it code
    }// end of touchView constructor

    public void findCirclePixels()
    {
        // ..... changePixel code
    }// end of changePixel()

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        // ...... event code
        return true;
    }// end of onTouchEvent

    private OnProgressChangeListener changeListener = new OnProgressChangeListener()
    {
        @Override
        public void onProgressChanged(View v, int progress)
        {
            // TODO Auto-generated method stub
            setProgress(progress);
        }

        private void setProgress(int progress)
        {
            Log.e(TAG, "******slidebar clicked*********** ");
        }
    };

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        canvas.drawBitmap(whitebm, 0, 0, null);
        canvas.drawBitmap(bgr, 0, 0, null);
        canvas.drawCircle(centreX, centreY, radius, pTouch);
    }// end of onDraw
}

.

05-13 16:01:17.911: ERROR/AndroidRuntime(2433): Uncaught handler: thread main exiting due to uncaught exception
05-13 16:01:17.936: ERROR/AndroidRuntime(2433): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.Jjilapp}: android.view.InflateException: Binary XML file line #9: Error inflating class com.tecmark.TouchView
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.os.Looper.loop(Looper.java:123)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at java.lang.reflect.Method.invoke(Method.java:521)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at dalvik.system.NativeStart.main(Native Method)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.tecmark.TouchView
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.Activity.setContentView(Activity.java:1622)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at com.tecmark.Jjilapp.onCreate(Jjilapp.java:30)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.app.ActivityThread.performLa开发者_Go百科unchActivity(ActivityThread.java:2459)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     ... 11 more
05-13 16:01:17.936: ERROR/AndroidRuntime(2433): Caused by: java.lang.reflect.InvocationTargetException
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at com.tecmark.TouchView.<init>(TouchView.java:59)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at java.lang.reflect.Constructor.constructNative(Native Method)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     ... 21 more
05-13 16:01:17.936: ERROR/AndroidRuntime(2433): Caused by: java.lang.NullPointerException
05-13 16:01:17.936: ERROR/AndroidRuntime(2433):     ... 25 more

.

Has anyone any ides why i'm getting this exception? Thanks matt.


You have

slider = (HorizontalSlider) this.findViewById(R.id.slider);

initialized inside your TouchView class' code, which I doubt to be correct, since the slider is not inside the TouchView, though it's inside the Activity, in the same layout the TouchView is.

You could remove the code from the TouchView's constructor, and put it inside a method public initSlider(HorizontalSlider slider).

Then in your Activity's onCreate method, you access both the TouchView and the HorizontalSlider views as

//TODO add id to your TouchView in xml!
final TouchView touchView = (TouchView)findViewById(R.id.touchView); 
final HorizontalSlider touchView = (HorizontalSlider)findViewById(R.id.slider); 

touchView.initSlider(slider);

And your TouchView's constructor will be splitted:

public TouchView(Context context, AttributeSet attr)
{
    super(context, attr);
    Log.e(TAG, "******inside touchview constructor ");
    // ......get bitmap and process it code
}// end of touchView constructor

public void initSlider(final HorizontalSlider slider)
{
    Log.e(TAG, "******setting up slider*********** ");
    slider.setOnProgressChangeListener(changeListener);
}


You're accessing your HorizontalSlider view from within the TouchView constructor. That HorizontalView hasn't even been instantiated yet. Even if it had been, it's likely not going to be located by a findViewByid() from the TouchView since it's not a child of the TouchView. I think you need to rethink your logic a bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜