Issue
I'm not really sure why my application won't start up, I get no errors and I have no idea what's going on? I'm pretty new to android and this is just a basic calculator, but it keeps crashing every time I start it up.
***************Main Activity.java**********************
package com.example.alex.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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);
new Something(this);
}
@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);
}
}
*******************Something.java******************
package com.example.alex.myapplication;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
public class Something extends Activity {
private Button add,subtract,multiply,devide;
private EditText editA, editB, editC;
private double doubleA,doubleB,doubleC;
public Something(Activity a) {
click();
}
public void click(){
editA = (EditText) findViewById(R.id.editText);
editB = (EditText) findViewById(R.id.editText2);
editC = (EditText) findViewById(R.id.editText3);
doubleA =Double.parseDouble(editA.getText().toString());
doubleB =Double.parseDouble(editB.getText().toString());
add = (Button) findViewById(R.id.add);
subtract = (Button) findViewById(R.id.subtract);
multiply = (Button) findViewById(R.id.multiply);
devide = (Button) findViewById(R.id.devide);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doubleC = doubleA+doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doubleC = doubleA-doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doubleC = doubleA*doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
devide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doubleC = doubleA/doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
}
}
******************activity_main.xml****************
<RelativeLayout 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" 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/idk"
android:textSize="17sp"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="5"
android:id="@+id/editText"
android:hint="First number"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="5"
android:id="@+id/editText2"
android:hint="Second number"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/add"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_marginBottom="166dp" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Subtract"
android:id="@+id/subtract"
android:layout_alignTop="@+id/multiply"
android:layout_alignLeft="@+id/add"
android:layout_alignStart="@+id/add" />
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Multiply"
android:id="@+id/multiply"
android:layout_marginTop="58dp"
android:layout_below="@+id/devide"
android:layout_alignRight="@+id/devide"
android:layout_alignEnd="@+id/devide" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Devide"
android:id="@+id/devide"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:id="@+id/editText3"
android:layout_below="@+id/textView"
android:hint="Results"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
</RelativeLayout>
Solution
The fact that you have created a constructor for the Something class
public Something(Activity a) {
click();
}
instead of using onCreate() is probably the source of the error.
Something is derived from Activity, in which you have to override onCreate() at the very least, and set the View of your Activity in it. You haven't done that, and instead you have created a constructor for Something which is also not allowed.
The solution to your problem is to replace the constructor with onCreate() and set the View of that Activity using setContentView(). Also, you cannot create an Activity object using
new Something(this);
Answered By - Yash Sampat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.