2012-11-10 35 views
0

我知道已經有很多關於如何解決致命異常的條目,但是他們都沒有解決我的問題。我正在研究一個非常簡單的計算器,它有九個按鈕,一個編輯文本和一個文本視圖。它的全部目的是簡單地鍵入一個數字,用另一個數字鍵擊一個按鈕,這些按鈕的產物將出現在文本視圖中。致命例外:Main java.lang.RuntimeException:

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

} 



} 
+0

你可以粘貼你的活動代碼嗎? –

+0

我的活動代碼? main.xml在那裏,這是不是一回事? – Lucreacia

+0

對不起,我是說清單文件。你忘了提到清單內的活動嗎? –

回答

2

從你的類刪除抽象的關鍵詞,它應該工作

+0

我以前試過,當它彈出錯誤。由於某種原因,它堅持認爲它需要「公共抽象類」當我刪除它,Eclipse出現一個錯誤,讀取「類型主要必須實現繼承的抽象方法View.onClickListener.onClick(視圖)」 – Lucreacia

+1

是的,我看到你有方法調用onclick,改變它toClickClick。 因爲你正在實現View.OnClickListener –

+0

......所有這一切都需要這種方法,是我有onclick而不是onClick?哇,Android可以真的很敏感,它現在完美運行!你這麼多! – Lucreacia

0

日誌明確表示

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

檢查你的Main類,它不應該是抽象的。

+0

當我嘗試刪除抽象詞,只是「公共類主」它告訴我「」類型主要必須實現繼承的抽象方法View.onClickListener.onClick(查看)「 – Lucreacia

+0

如果你不想在onClickListener實現方法爲什麼你想添加在第一個地方? –

相關問題