我不确定为什么我的应用程序无法启动,我没有错误,我不知道发生了什么?我对android很新,这只是一个基本的计算器,但每次启动时它都会崩溃。
***************主要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>
答案 0 :(得分:1)
您已为Something
类
public Something(Activity a) {
click();
}
而不是使用onCreate()
可能是错误的来源。
Something
源自Activity
,您 至少要覆盖onCreate()
,并设置您的View
其中Activity
。你没有这样做,而是你已经为Something
创建了一个构造函数,这也是不允许的。
问题的解决方案是将构造函数替换为onCreate()
,并使用View
设置Activity
的{{1}}。此外,您无法使用
setContentView()
对象
Activity