[英]SeekBar listenner crashes app when in a fragment
I have a seekbar inside a fragment and when I run the app it crashes.我在片段中有一个搜索栏,当我运行应用程序时它崩溃了。 The fragment is part of the main activity.
该片段是主要活动的一部分。 Here is the code:
这是代码:
public class PalleteFragment extends Fragment {
public TextView textView;
private CanvasView canvas;
public PalleteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pallete, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
textView = (TextView) view.findViewById(R.id.textView);
textView.setText("Pen size: " + seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
canvas.setStrokeWidth(seekBar.getProgress());
textView.setText("Pen size: " + seekBar.getProgress());
}
...
});
}
}
Is something missing that I need to write?我需要写些什么吗?
Here is the error that occurs when the app crashes:这是应用程序崩溃时发生的错误:
This is the CanvasView class in short:简而言之,这是 CanvasView 类:
public class CanvasView extends View implements View.OnTouchListener {
private Paint mPaint;
private Path mPath;
private int mDrawColor;
private int mBackgroundColor;
private Canvas mExtraCanvas;
private Bitmap mExtraBitmap;
private int strokeWidth;
private GestureDetector mGestureDetector;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
CanvasView(Context context) {
super(context);
setOnTouchListener(this);
strokeWidth = 10;
mBackgroundColor = ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null);
mDrawColor = ResourcesCompat.getColor(getResources(),
R.color.simpleBlack, null);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(mDrawColor);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE); // default: FILL
mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
mPaint.setStrokeWidth(strokeWidth);
}
public CanvasView(Context context, AttributeSet attributeSet,GestureDetector mGestureDetector) {
super(context);
this.mGestureDetector = mGestureDetector;
strokeWidth = 10;
setOnTouchListener(this);
mBackgroundColor = ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null);
mDrawColor = ResourcesCompat.getColor(getResources(),
R.color.simpleBlack, null);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(mDrawColor);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE); // default: FILL
mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
mPaint.setStrokeWidth(strokeWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mExtraBitmap, 0, 0, null);
mPaint.setStrokeWidth(strokeWidth);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
updateColor(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt("color",getResources().getColor(R.color.simpleBlack)));
setStrokeWidth(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt("stroke", 0));
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
break;
default:
// Do nothing.
}
return true;
}
private void touchStart(float x, float y) {
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
mExtraCanvas.drawPath(mPath, mPaint);
}
}
private void touchUp() {
mPath.reset();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return false;
}
public void erase(){
mExtraCanvas.drawColor(ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null));
}
public void setStrokeWidth(int width) {
mPaint.setStrokeWidth(width);
}
} }
The class is super long so I ommited other methods that weren't necessary for this question.这门课很长,所以我省略了这个问题不需要的其他方法。
Here is the XML from the fragment:这是片段中的 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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pen Size: " />
</LinearLayout>
Here is the app's directories:这是应用程序的目录:
Use your CanvasView inside your fragment's xml file:在片段的 xml 文件中使用 CanvasView:
<com.lionsilva.newpaint.customviews.CanvasView
android:id="@+id/canvas_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then initialize it like any other view:然后像任何其他视图一样初始化它:
private CanvasView canvas;
canvas = view.findViewById(R.id.canvas_view);
canvas.setStrokeWidth( seekBar.getProgress() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.