2012-11-10 2 views
0

. 9 개의 버튼, 편집 텍스트 및 텍스트보기가있는 매우 간단한 계산기로 작업하고 있습니다. 그것의 전체적인 목적은 단순히 숫자를 입력 할 수 있고, 다른 숫자가있는 버튼을 누르면 그 제품이 텍스트보기에 나타날 것입니다.치명적인 예외 : 홈페이지 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> 

자바 코드 :

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

이 클래스에서 추상적 인 키워드를 제거하고 로그 명확

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

을 말한다

+0

이전에 오류가 발생했을 때 그것을 시도했습니다. 어떤 이유로 그것이 그것을 주장합니다 나는 그것을 제거하면 이클립스는 "유형 주요 상속 된 추상 메소드 View.onClickListener.onClick (보기)를 구현해야합니다"라는 오류와 함께 온다 "공공 추상 클래스를"필요 – Lucreacia

+1

가 네 난 당신이 방법은 온 클릭 불리는이보고, 변경 당신이 View.OnClickListener –

+0

구현하기 때문에 온 클릭합니다. 당신은 그것이 모두가 나를 onclick을 대신 onclick을 가지고 있었다 ...이 방법이 필요하십니까? 와우, 안드로이드 정말 민감한 될 수 있습니다. 그것은 지금 완벽 실행! 감사 요 너무 많이! – Lucreacia

0

작동합니다 Main 클래스를 확인하십시오. 추상적이어서는 안됩니다.

+0

나는 단순히 "나에게 말한다"공공 클래스 메인 ""유형 주요 상속 된 추상 메소드 View.onClickListener.onClick (보기)를 구현해야합니다 "하는 추상적 인 단어를 제거하려고하면 – Lucreacia

+0

당신은 OnClickListener를 왜의 방법을 구현하기 위해 싶지 않는 경우 첫 번째 장소에 추가 하시겠습니까? –