该程序将无法运行,但没有错误

时间:2012-11-07 23:13:19

标签: android eclipse

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

     }



}

2 个答案:

答案 0 :(得分:2)

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

abstract class?为什么?您永远无法实现抽象Activity

删除abstract声明,它会正常工作。并确保您已在应用程序的清单中声明Main活动。

答案 1 :(得分:0)

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

  1. 实例化单独的 OnClickListener对象以处理点击次数。你这样做的方式不会起作用,并且会引起头痛。它违背了Android的整个MVC性质,我不会在这里讨论,但总之,你要求Controller执行View的工作。请相信我。

  2. 键入方法名称时请注意区分大小写。您的方法名称是onclick(View v)。方法名称必须为onClick(View v)(注意大写字母C)。 Java是一种区分大小写的语言,因此永远不会调用您的方法。

  3. (你的Activity类不能像其他人指出的那样被声明为抽象,但听起来你已经解决了这个问题。)

    请参阅我在评论前面的评论

    示例:

    //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...
    }
    }