Thursday, 8 January 2015

Rectangle not appearing on the right



I'm trying to create a rectangle within a canvas so that it appears on the right hand side of the screen but the app seems to crash every time I run it. I seriously don't know am I doing wrong here + what needs to change within my code?


activity_main.xml



<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.apptacularapps.customview.DrawView
android:id="@+id/diagram"
android:layout_width="fill_parent"
android:layout_height="52dp" />

</LinearLayout>


MainActivity.java



package com.apptacularapps.customview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DrawView drawView = new DrawView(this);
drawView.setBackgroundColor(Color.BLACK);
setContentView(drawView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


DrawView.java



package com.apptacularapps.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View {
Paint paint = new Paint();
Context context;

public DrawView(Context context) {
super(context);
this.context = context;
}

@Override
public void onDraw(Canvas canvas) {

//Code to Measure the Screen width in pixels

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;

paint.setColor(Color.RED);
canvas.drawRect(0, 0, 5, canvas.getHeight(), paint );

paint.setColor(Color.RED);
canvas.drawRect(canvas.getWidth()-width, 0, 5, canvas.getHeight(), paint );
}
}


Logcat



01-08 13:03:18.331 2336-2336/com.apptacularapps.customview I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-08 13:03:18.400 2336-2336/com.apptacularapps.customview W/ActivityThread﹕ Application com.apptacularapps.customview is waiting for the debugger on port 8100...
01-08 13:03:18.403 2336-2336/com.apptacularapps.customview I/System.out﹕ Sending WAIT chunk
01-08 13:03:18.809 2336-2343/com.apptacularapps.customview I/art﹕ Debugger is active
01-08 13:03:18.824 2336-2336/com.apptacularapps.customview I/System.out﹕ Debugger has connected
01-08 13:03:18.824 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:19.034 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:19.244 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:19.453 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:19.665 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:19.875 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:20.084 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:20.293 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:20.504 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:20.714 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:20.924 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:21.134 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:21.344 2336-2336/com.apptacularapps.customview I/System.out﹕ waiting for debugger to settle...
01-08 13:03:21.556 2336-2336/com.apptacularapps.customview I/System.out﹕ debugger has settled (1325)
01-08 13:03:21.647 2336-2336/com.apptacularapps.customview D/AndroidRuntime﹕ Shutting down VM
01-08 13:03:21.648 2336-2336/com.apptacularapps.customview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apptacularapps.customview, PID: 2336
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apptacularapps.customview/com.apptacularapps.customview.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class com.apptacularapps.customview.DrawView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.apptacularapps.customview.DrawView
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.apptacularapps.customview.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:580)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.apptacularapps.customview.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-08 13:03:24.857 2336-2336/com.apptacularapps.customview I/Process﹕ Sending signal. PID: 2336 SIG: 9

No comments:

Post a Comment