有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓致命异常:主java。lang.RuntimeException:

我知道已经有很多关于如何解决致命异常的条目,但是没有一个解决了我的问题。我正在开发一个非常简单的计算器,它有九个按钮、一个编辑文本和一个文本视图。它的全部目的就是简单地输入一个数字,点击一个按钮,按钮上有另一个数字,这些数字的乘积将出现在文本视图中

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" >

<TextView
    安卓:id="@+id/textView1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentLeft="true"
    安卓:layout_alignParentTop="true"
    安卓:layout_marginTop="16dp"
    安卓:text="@string/number" />

<EditText
    安卓:id="@+id/editText1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_below="@+id/textView1"
    安卓:layout_centerHorizontal="true"
    安卓:layout_marginTop="16dp"
    安卓:ems="10"
    安卓:inputType="numberDecimal" />

<Button
    安卓:id="@+id/button1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentLeft="true"
    安卓:layout_below="@+id/editText1"
    安卓:layout_marginLeft="16dp"
    安卓:layout_marginTop="23dp"
    安卓:text="@string/s1" />

<Button
    安卓:id="@+id/button2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button1"
    安卓:layout_alignBottom="@+id/button1"
    安卓:layout_centerHorizontal="true"
    安卓:text="@string/s2" />

<Button
    安卓:id="@+id/button3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button2"
    安卓:layout_alignBottom="@+id/button2"
    安卓:layout_alignRight="@+id/textView1"
    安卓:text="@string/s3" />

<Button
    安卓:id="@+id/button4"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/button1"
    安卓:layout_below="@+id/button1"
    安卓:layout_marginTop="16dp"
    安卓:text="@string/s4" />

<Button
    安卓:id="@+id/button5"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button4"
    安卓:layout_alignBottom="@+id/button4"
    安卓:layout_alignLeft="@+id/button2"
    安卓:text="@string/s5" />

<Button
    安卓:id="@+id/button6"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button5"
    安卓:layout_alignBottom="@+id/button5"
    安卓:layout_alignLeft="@+id/button3"
    安卓:text="@string/s6" />

<Button
    安卓:id="@+id/button7"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/button4"
    安卓:layout_below="@+id/button4"
    安卓:layout_marginTop="22dp"
    安卓:text="@string/s7" />

<Button
    安卓:id="@+id/button8"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button7"
    安卓:layout_alignBottom="@+id/button7"
    安卓:layout_alignLeft="@+id/button5"
    安卓:text="@string/s8" />

<Button
    安卓:id="@+id/button9"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBaseline="@+id/button8"
    安卓:layout_alignBottom="@+id/button8"
    安卓:layout_alignLeft="@+id/button6"
    安卓:text="@string/s9" />

<TextView
    安卓:id="@+id/textView2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/button8"
    安卓:layout_below="@+id/button8"
    安卓: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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.os.Handler.dispatchMessage(Handler.java:99)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.os.Looper.loop(Looper.java:123)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-10 11:50:13.198: E/AndroidRuntime(451):  at com.安卓.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 安卓.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-10 11:50:13.198: E/AndroidRuntime(451):  at 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.deitel.multiplicationtables"
安卓:versionCode="1"
安卓:versionName="1.0" >

<uses-sdk
    安卓:minSdkVersion="8"
    安卓:targetSdkVersion="15" />

<application
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name=".Main"
        安卓:label="@string/title_activity_main" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

JAVA代码:

package com.deitel.multiplicationtables;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.EditText;
import 安卓.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) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    日志上写得很清楚

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

    检查Main类,它不应该是抽象的