Lucreacia
Lucreacia

Reputation: 79

The program won't run, but there is no error

I searched my problem and found someone else with a very similar one, but their solution is not the same. I wrote a code to do the simple task of multiplication depending on the button the user hits. The main.xml file and the accompanying java file are both error free, no warnings are even on the pages. Everything looks great, but when I try to run the program it pops up telling me that there are errors and to please fix them. Nothing appears on the console nor in the LogCat. When I go to windows -> show view -> problems, it won't list anything pertaining to that program either.

My main.xml code is:

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

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

     }



}

Upvotes: 0

Views: 136

Answers (2)

Nik
Nik

Reputation: 76

I'm not going to check all of your multiplication logic, but the heart of your question is around why the button click is not working. You need to do two more things:

  1. Instantiate a separate OnClickListener object to handle the clicks. The way you're doing it won't work and is going to cause headaches. It goes against the whole MVC nature of Android, which I won't get into here, but in short you're asking a Controller to do a View's job. Just trust me here.

  2. Pay attention to case-sensitivity when typing method names. Your method name is onclick(View v). The method name must be onClick(View v) (note the capital letter C). Java is a case-sensitive language, so you're method will never be invoked.

(Your Activity class cannot be declared as abstract as others have pointed out, but it sounds like you've fixed this.)

See my comments preceded by NOTE comments

Example:

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

Upvotes: 0

yugidroid
yugidroid

Reputation: 6690

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

abstract class? Why? You will never be able to instanciate an abstract Activity!

Remove the abstract declaration and it will work fine. And make sure you have declared Main acivity at th application's manifest.

Upvotes: 2

Related Questions