Reputation: 11
am facing the null pointer Exception.In Xml i add two images and drawView in the layout.By clicking the Image i want to draw that color on the surface but its not working . Refer this code and solve the error plz.
**MainActivity:**
public class `MainActivity` extends Activity {
private Paint mPaint;
View content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*layout.addView(mView, new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT)); */
init();
ImageView next = ( ImageView ) findViewById(R.id.imageView1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.GREEN);
}
});
DrawView drawingSurface = (DrawView) findViewById(R.id.drawingSurface);
content.setDrawingCacheEnabled(true);
}
private void init() {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
}
**DrawView.java**
public class DrawView extends View {
private Path path;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
public DrawView(Context context, AttributeSet attrs) {
super(context,attrs);
path = new Path();
mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
this.setBackgroundColor(Color.BLACK);
}
private ArrayList<PathWithPaint> _graphics1 = new ArrayList<PathWithPaint>();
@Override
public boolean onTouchEvent(MotionEvent event) {
PathWithPaint pp = new PathWithPaint();
mCanvas.drawPath(path, mPaint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
pp.setPath(path);
pp.setmPaint(mPaint);
_graphics1.add(pp);
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (_graphics1.size() > 0) {
canvas.drawPath(
_graphics1.get(_graphics1.size() - 1).getPath(),
_graphics1.get(_graphics1.size() - 1).getmPaint());
}
}
}
**pathwithpaint.java**
public class PathWithPaint {
private Path path;
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
private Paint mPaint;
public Paint getmPaint() {
return mPaint;
}
public void setmPaint(Paint mPaint) {
this.mPaint = mPaint;
}
}
Logcat
Errors
03-19 19:08:36.997: E/AndroidRuntime(478): FATAL EXCEPTION: main
03-19 19:08:36.997: E/AndroidRuntime(478): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex/com.example.ex.MainActivity}: java.lang.NullPointerException
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.os.Looper.loop(Looper.java:123)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-19 19:08:36.997: E/AndroidRuntime(478): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 19:08:36.997: E/AndroidRuntime(478): at java.lang.reflect.Method.invoke(Method.java:521)
03-19 19:08:36.997: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-19 19:08:36.997: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-19 19:08:36.997: E/AndroidRuntime(478): at dalvik.system.NativeStart.main(Native Method)
03-19 19:08:36.997: E/AndroidRuntime(478): Caused by: java.lang.NullPointerException
03-19 19:08:36.997: E/AndroidRuntime(478): at com.example.ex.MainActivity.onCreate(MainActivity.java:48)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 19:08:36.997: E/AndroidRuntime(478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-19 19:08:36.997: E/AndroidRuntime(478): ... 11 more
Xml File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myDrawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<com.example.ex.DrawView
android:id="@+id/drawingSurface"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="11dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="67dp"
android:layout_marginRight="16dp"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Upvotes: 0
Views: 160
Reputation: 17085
View content
in activity is not initialized. so when you try to access
content.setDrawingCacheEnabled(true);
it will be null.
Upvotes: 1