致命异常:主java.lang.RuntimeException:

时间:2012-11-10 18:26:26

标签: java android exception java.lang.class

我知道已经有很多关于如何解决致命异常的条目,但它们都没有解决我的问题。我正在研究一个非常简单的计算器,它有九个按钮,一个编辑文本和一个文本视图。它的全部目的是简单地键入一个数字,点击一个带有另一个数字的按钮,它们的产品将出现在文本视图中。

<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);

}



}

2 个答案:

答案 0 :(得分:2)

从类中删除抽象关键字,它应该可以正常工作

答案 1 :(得分:0)

日志明确说明

Caused by: java.lang.InstantiationException: com.deitel.multiplicationtables.Main

检查你的Main课程,它不应该是抽象的。