我有一个应用程序,我的视图从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>
我的视图的构造函数包含传递从xml扩展所需的AttributeSet的版本。
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
有没有人为什么我得到这个例外?谢谢亚光。
答案 0 :(得分:2)
你有
slider = (HorizontalSlider) this.findViewById(R.id.slider);
在您的TouchView
类代码中初始化,我怀疑是正确的,因为slider
不在 TouchView
内,尽管它位于Activity
内TouchView
,在TouchView
的相同布局中。{/ p>
您可以从public initSlider(HorizontalSlider slider)
的构造函数中删除代码,并将其放在方法Activity
中。
然后,在onCreate
的{{1}}方法中,您可以同时访问TouchView
和HorizontalSlider
次观看
//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);
}
答案 1 :(得分:-1)
您正在HorizontalSlider
构造函数中访问TouchView
视图。那个HorizontalView还没有被实例化。即使它已经存在,也可能不会被findViewByid()
TouchView
定位,因为它不是TouchView
的孩子。我想你需要重新考虑一下你的逻辑。