0

我搜索了我的问题,发现其他人的问题非常相似,但他们的解决方案不一样。我编写了一个代码来执行简单的乘法任务,具体取决于用户点击的按钮。main.xml 文件和随附的 java 文件都没有错误,甚至页面上也没有警告。一切看起来都很棒,但是当我尝试运行该程序时,它会弹出告诉我有错误并请修复它们。控制台和 LogCat 中均未显示任何内容。当我转到 windows -> 显示视图 -> 问题时,它也不会列出与该程序有关的任何内容。

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

我的爪哇:

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

我不会检查你所有的乘法逻辑,但你问题的核心是为什么按钮点击不起作用。你还需要做两件事:

  1. 实例化一个单独的OnClickListener 对象来处理点击。你这样做的方式是行不通的,而且会引起头痛。它违背了 Android 的整个 MVC 特性,我不会在这里讨论,但简而言之,您是在要求控制器完成视图的工作。在这里相信我。

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