0

私は自分の問題を検索し、非常に似た問題を抱えている他の人を見つけましたが、彼らの解決策は同じではありません. ユーザーが押すボタンに応じて乗算という単純なタスクを実行するコードを作成しました。main.xml ファイルと付随する Java ファイルはどちらもエラーがなく、ページにも警告はありません。すべて問題ないように見えますが、プログラムを実行しようとすると、エラーがあるので修正してくださいというポップアップが表示されます。コンソールにも LogCat にも何も表示されません。ウィンドウ -> ビューの表示 -> 問題に移動しても、そのプログラムに関連するものは何も表示されません。

私の main.xml コードは次のとおりです。

<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/1" />

<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/2" />

<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/3" />

<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/4" />

<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/5" />

<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/6" />

<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/7" />

<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/8" />

<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/9" />

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

私の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; 
    final double two = 2;
    final double three = 3;
    final double four = 4; 
    final double five = 5;
    final double six = 6;
    final double seven = 7; 
    final double eight = 8;
    final double nine = 9;

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

     }



}
4

2 に答える 2

2
public abstract class Main extends Activity implements View.OnClickListener{
    ...
}

abstract class?なんで?アブストラクトをインスタンス化することはできませんActivity

abstract宣言を削除すると、正常に機能します。Mainまた、アプリケーションのマニフェストでアクティビティを宣言していることを確認してください。

于 2012-11-07T23:29:09.973 に答える
0

すべての乗算ロジックをチェックするつもりはありませんが、質問の核心は、ボタンのクリックが機能しない理由です。さらに 2 つのことを行う必要があります。

  1. クリックを処理するために、別のOnClickListener オブジェクトをインスタンス化します。あなたがやっている方法はうまくいかず、頭痛の原因になります。これは Android の MVC の性質全体に反するため、ここでは説明しませんが、要するに、View の仕事を Controller に依頼しているのです。ここで私を信じてください。

  2. メソッド名を入力するときは、大文字と小文字の区別に注意してください。メソッド名は onclick(View v) です。メソッド名は onClick(View v) でなければなりません (大文字の C に注意してください)。Java は大文字と小文字を区別する言語であるため、メソッドが呼び出されることはありません。

(あなたの Activity クラスは、他の人が指摘しているように抽象として宣言することはできませんが、これを修正したようです。)

NOTE コメントの前に私のコメントを参照してください

例:

//NOTE Main no longer implements View.OnClickListener
public class Main extends Activity {
// 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);

//NOTE seperate onClickListener()
OnClickListener oc = new OnClickListener(){

    @Override
    //creates a method (or action) for when the button is clicked.
    public void onClick(View view) //NOTE the capital C in Click
    {
        //Makes a variable for the entered number
        Double amount = 0.0;
        Double product = 0.0;
        Double variable = 0.0;

        // constants
        final double one = 1; 
        final double two = 2;
        final double three = 3;
        final double four = 4; 
        final double five = 5;
        final double six = 6;
        final double seven = 7; 
        final double eight = 8;
        final double nine = 9;

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

    }
};

//NOTE setting the seperate OnClickListener
//Adds the buttons to the onclicklistener
bone.setOnClickListener(oc);
btwo.setOnClickListener(oc);
bthree.setOnClickListener(oc);
bfour.setOnClickListener(oc);
bfive.setOnClickListener(oc);
bsix.setOnClickListener(oc);
bseven.setOnClickListener(oc);
beight.setOnClickListener(oc);
bnine.setOnClickListener(oc);

//...snip...
}
}
于 2014-03-05T16:27:10.490 に答える