致命的な例外を解決する方法についてはすでに多数のエントリがあることは知っていますが、どれも私の問題を解決しませんでした。私は、9つのボタン、編集テキスト、およびテキストビューを備えた非常に単純な計算機に取り組んでいます。それの全体的な目的は、単に数字を入力し、その上に別の数字が付いたボタンを押すことができるようにすることであり、それらの積がテキストビューに表示されます。
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="@string/number" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="16dp"
android:layout_marginTop="23dp"
android:text="@string/s1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="@string/s2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignRight="@+id/textView1"
android:text="@string/s3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:text="@string/s4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:text="@string/s5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_alignLeft="@+id/button3"
android:text="@string/s6" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginTop="22dp"
android:text="@string/s7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignLeft="@+id/button5"
android:text="@string/s8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_alignLeft="@+id/button6"
android:text="@string/s9" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_below="@+id/button8"
android:layout_marginTop="70dp"
/>
</RelativeLayout>
私はついにすべてのねじれを解決したかと思いましたが、プログラムを実行しようとすると、ロードする前に予期せず停止します。
11-10 11:50:13.198: E/AndroidRuntime(451): FATAL EXCEPTION: main
11-10 11:50:13.198: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.deitel.multiplicationtables/com.deitel.multiplicationtables.Main}: java.lang.InstantiationException: com.deitel.multiplicationtables.Main
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:507)
11-10 11:50:13.198: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-10 11:50:13.198: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-10 11:50:13.198: E/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): Caused by: java.lang.InstantiationException: com.deitel.multiplicationtables.Main
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.Class.newInstanceImpl(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.Class.newInstance(Class.java:1409)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
11-10 11:50:13.198: E/AndroidRuntime(451): ... 11 more
11-10 11:55:13.367: I/Process(451): Sending signal. PID: 451 SIG: 9
見落としが少ないかもしれませんが、めちゃくちゃになっているのがわかりません。どんな援助も大歓迎です!
マニフェスト:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deitel.multiplicationtables"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
JAVAコード:
package com.deitel.multiplicationtables;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
//Implements the listener for an onclick event (implements View.onClickListener)
public abstract class Main extends Activity implements View.OnClickListener{
// creates a button
private Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//assigns the resource id of 1 - 9 to each button.
bone = (Button) findViewById(R.id.button1);
btwo = (Button) findViewById(R.id.button2);
bthree = (Button) findViewById(R.id.button3);
bfour = (Button) findViewById(R.id.button4);
bfive = (Button) findViewById(R.id.button5);
bsix = (Button) findViewById(R.id.button6);
bseven = (Button) findViewById(R.id.button7);
beight = (Button) findViewById(R.id.button8);
bnine = (Button) findViewById(R.id.button9);
//Adds the buttons to the onclicklistener
bone.setOnClickListener(this);
btwo.setOnClickListener(this);
bthree.setOnClickListener(this);
bfour.setOnClickListener(this);
bfive.setOnClickListener(this);
bsix.setOnClickListener(this);
bseven.setOnClickListener(this);
beight.setOnClickListener(this);
bnine.setOnClickListener(this);
}
//creates a method (or action) for when the button is clicked.
public void onclick(View view)
{
//Makes a variable for the entered number
Double amount = 0.0;
Double product = 0.0;
Double variable = 0.0;
// constants
final double one = 1.0;
final double two = 2.0;
final double three = 3.0;
final double four = 4.0;
final double five = 5.0;
final double six = 6.0;
final double seven = 7.0;
final double eight = 8.0;
final double nine = 9.0;
if (view.getId() == R.id.button1)
{
variable = one;
}
if (view.getId() == R.id.button2)
{
variable = two;
}
if (view.getId()== R.id.button3)
{
variable = three;
}
if (view.getId() == R.id.button4)
{
variable = four;
}
if (view.getId() == R.id.button5)
{
variable = five;
}
if (view.getId()== R.id.button6)
{
variable = six;
}
if (view.getId() == R.id.button7)
{
variable = seven;
}
if (view.getId() == R.id.button8)
{
variable = eight;
}
if (view.getId()== R.id.button9)
{
variable = nine;
}
//creates an editext and assigns the resource id of the xml edittext.
EditText number = (EditText)findViewById(R.id.editText1);
//Receives the input from the edittext, converts it to a double (number).
amount = Double.parseDouble(number.getText().toString());
//Calculates the product
product = variable * amount;
//Creates a textview object, assigns the xml r.id, and then changes the text to report the amount.
TextView t = (TextView)findViewById(R.id.textView2);
t.setText("Your product is: " + product);
}
}