Basically I am trying to gather the size of room from the user..(example 20x20 size room). Using those dimensions, collected on the mainActivity, send that data to the Result Activity and use the onDraw/onMeasure to create a simple box.
In short, I want to collect the size of the box in mainActivity from the user. Then in Result activity show the completed image of the box.
I have completed the passing Intents with data from one activity to another. Its using that collected data to draw a shape I am having trouble with.
public class MainActivity extends AppCompatActivity {
//variables
Button btnCalculate, btnReset;
Spinner spinSpeaker;
EditText editLength, editWidth, editHeight;
String t1,t2,t3;
int numLength, numWidth, numHeight, numResult=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//connecting variables to GUI elements
btnCalculate= findViewById(R.id.btnCalculate);
btnReset = findViewById(R.id.btnReset2);
spinSpeaker = findViewById(R.id.spinSpeaker);
editLength= findViewById(R.id.editLength);
editWidth = findViewById(R.id.editWidth);
editHeight = findViewById(R.id.editHeight);
//adding function to the calculate button
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//parsing text to integers
t1=editLength.getText().toString();
t2=editWidth.getText().toString();
t3=editHeight.getText().toString();
if (t3.equals("")) {
editHeight.setText("11");
}else if (t2.equals("")) {
editWidth.setText("11");
}else if (t1.equals("")) {
editLength.setText("11");
}else{
numLength = Integer.parseInt(t1);
numWidth = Integer.parseInt(t2);
numHeight = Integer.parseInt(t3);
}
//alert to contact Pro design
if ((numLength>=999)||(numWidth>=999)||(numHeight>=20)) {
Toast.makeText(MainActivity.this, R.string.contactPro,
Toast.LENGTH_LONG).show();
}else {
//calculating volume
numResult = numHeight * numWidth * numLength;
//sending data to other activities
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("varHeight", numHeight);
intent.putExtra("varWidth", numWidth);
intent.putExtra("varLength", numLength);
intent.putExtra("varResult", numResult);
startActivity(intent);
}
}
});
}
//Result activity
public class Result extends AppCompatActivity {
TextView tim;
MyCanvas mycanvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mycanvas = new MyCanvas(this);
setContentView(R.layout.activity_result);
setContentView(Jim);
//Pulling variables from main activity
Intent variables = getIntent();
//int length = variables.getIntExtra("varLength",0);
//int width =variables.getIntExtra("varWidth",0);
int totalResult = variables.getIntExtra("varResult",0);
//showing total
String result = String.valueOf(totalResult);
tim = findViewById(R.id.txtResult);
tim.setText(result);
//Calling the reset button method
configureResetBtn();
}
public void configureResetBtn(){
Button btnReset=findViewById(R.id.btnReset2);
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//startActivity(new Intent(Result.this, MainActivity.class));
finish();
}
});
}
}
//MyCanvas activity
public class MyCanvas extends AppCompatActivity {
public MyCanvas(Result result) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent variables = getIntent();
final int length = variables.getIntExtra("varLength", 0);
final int width = variables.getIntExtra("varWidth", 0);
class Jim extends View {
Paint paint = new Paint();
Rect rect = new Rect();
public Jim(Context context) {
super(context);
}
@Override
protected void onDraw(android.graphics.Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(length,width);
}
}
}
}
The expected result is to show a box that is the dimensions based on the user (example 20x20). However I am getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
But my button works when I remove code trying to draw the box.
Instead of setting content view twice
In ResultActivity
use SurfaceView
in R.layout.activity_result
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//....
</FrameLayout>
In ResultActivity
SurfaceView surfaceView = findViewById(R.id.surface_view);
surfaceView.getHolder().addCallback(new Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
// draw
Paint paint = new Paint();
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
});