我搜索了我的问题,发现其他人的问题非常相似,但他们的解决方案不一样。我编写了一个代码来执行简单的乘法任务,具体取决于用户点击的按钮。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);
}
}