我有一个应用程序,其中的视图从xml扩展了。在尝试添加“水平”滑动条之前,它工作正常,该组件扩展了progressBar。我已将进度条代码添加到正确包中的src文件夹中,并在相对布局中的布局文件中声明了它。

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


我的视图的构造函数包括传递AttributeSet的版本,而AttributeSet需要从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.performLaunchActivity(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




有谁能给我一个例外?谢谢马特。

最佳答案

你有

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


TouchView类的代码中进行了初始化,我怀疑这是正确的,因为slider不在TouchView内,尽管它在Activity内,但与TouchView布局相同。

您可以从TouchView的构造函数中删除代码,并将其放在方法public initSlider(HorizontalSlider slider)中。

然后,在ActivityonCreate方法中,以以下方式访问TouchViewHorizontalSlider视图:

//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);


您的TouchView的构造函数将被拆分:

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);
}

10-04 11:58