0

I am new to Android developing and I am trying to make a simple app as practice. All it consists of is you type one number into the first box, a number into the second box, press a button and the result prints into a textview. The problem is, when I press the send button Nothing is happening. Eclipse is not showing any errors, nor is LogCat. So I am officially stumped as to why when I run this I'm not getting anything out. Hopefully you guys can help.

Here is the 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="172dp"
    android:orientation="vertical" >
</LinearLayout>

<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_marginLeft="16dp"
    android:layout_marginTop="48dp"
    android:text="First Number:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="40dp"
    android:text="Second Number:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignTop="@+id/textView1"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView2"
    android:layout_toRightOf="@+id/linearLayout1"
    android:ems="10"
    android:inputType="number" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:text="Calc" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:text="TextView" />

</RelativeLayout>

And here is the Java:

package com.example.mathbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

 public class MainActivity extends Activity { 

    Button sum;
    EditText num1, num2;
    TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void main(String[] args){

    num1 = (EditText) findViewById(R.id.editText1);
    num2 = (EditText) findViewById(R.id.editText2);
    tv = (TextView) findViewById(R.id.textView3);

num1.getText().toString();
num2.getText().toString();

    int x =Integer.parseInt("num1");
    int y =Integer.parseInt("num2");

    int nas = x + y;

    tv.setText(nas);



    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {}

});}}
4

8 回答 8

1

您正在解析错误的字符串..

num1.getText().toString();
num2.getText().toString();

int x =Integer.parseInt("num1");
int y =Integer.parseInt("num2");

您现在正在做的是将字符串“num1”和“num2”解析为字符串,我认为这不是您想要的。所以应该是:

int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());

并且像@AndroidEnthusiastic 建议的那样将代码移动到 onCreate() 方法中。

于 2013-04-24T06:09:51.747 回答
0

Hye Diamond,你做错了什么。没关系,你的初学者。看到您正在创建主要方法。在 java main 方法中public static void main首先执行,但是一旦创建了 oncreate 方法的活动就会执行。因此,您可以通过以下方式进行操作:

 //inside oncreate method do like this
  num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
 findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) 
    {
   int val1 = num1.getText().toString();
   int val2 = num2.getText().toString();

int x =Integer.parseInt(val1);
int y =Integer.parseInt(val2);

int nas = x + y;

tv.setText(nas);
    }

});

这可能对你有帮助。它没有经过测试。可能你必须做一些改变。

于 2013-04-24T06:16:32.927 回答
0

找到这个代码..希望它有帮助

public class MainActivity extends Activity {

Button sum;
EditText num1, num2;
TextView tv;
String x;
String y;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    num1 = (EditText) findViewById(R.id.editText1);
    num2 = (EditText) findViewById(R.id.editText2);
    tv = (TextView) findViewById(R.id.textView3);
    sum = (Button) findViewById(R.id.button1);

    sum.setOnClickListener(new clicker());
}

class clicker implements Button.OnClickListener {
    public void onClick(View v) {

        Integer nas;
        x = num1.getText().toString();
        y = num2.getText().toString();
        nas = Integer.parseInt(x) + Integer.parseInt(y);
        tv.setText(nas.toString());
    }
}
}
于 2013-04-24T06:35:36.863 回答
0
public void main(String[] args)

对于 Android,这是一个简单的方法,而不是应该是的入口点:

public static void main(String[] args)

我看不到您在代码中调用 main 方法。

num1.getText().toString();
num2.getText().toString();

您只需获取值而不将其分配给任何属性。
这段代码是完全错误的。

我建议您从官方 Android 培训开始:构建您的第一个应用程序

于 2013-04-24T06:12:34.280 回答
0

这是完整的解决方案

package com.example.mathbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity { 

    Button sum;
    EditText num1, num2;
    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        num1 = (EditText) findViewById(R.id.editText1);
        num2 = (EditText) findViewById(R.id.editText2);
        tv = (TextView) findViewById(R.id.textView3);

        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                num1.getText().toString();
                num2.getText().toString();

                int x =Integer.parseInt("num1");
                int y =Integer.parseInt("num2");

                int nas = x + y;

                tv.setText(nas);
            }
        });
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

请看一下这个链接

于 2013-04-24T06:18:05.943 回答
-1

请尝试此代码,

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class add extends Activity {
Button     button1;
 EditText   txtbox1,txtbox2;
 TextView tv;

  /** Called when the activity is first created. */
   @Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 txtbox1=  (EditText) findViewById(R.id.txtbox1);
 button1 = (Button) findViewById(R.id.button1);
 tv = (TextView) findViewById(R.id.lbl1);
 txtbox2=  (EditText) findViewById(R.id.txtbox2);
 button1.setOnClickListener(new clicker());    
 }
class  clicker implements Button.OnClickListener
{           
public void onClick(View v)
{
 String a,b;
 Integer vis;
 a =  txtbox1.getText().toString();
 b =  txtbox2.getText().toString();    
 vis =  Integer.parseInt(a)+Integer.parseInt(b);    
 tv.setText(vis.toString());    
    }
    }
  }
于 2013-04-24T06:09:51.493 回答
-1

请不要使用 main 函数。这在活动课中并不是真正需要的,也不是一般做法。@AndroidEnthusiastic 给出了正确的代码。

补充:由于我的上述答案被否决了,我会继续告诉你你在代码中做错了什么(这里给出的其他答案中有很多代码)。

            num1 = (EditText) findViewById(R.id.editText1);
            num2 = (EditText) findViewById(R.id.editText2);
            tv = (TextView) findViewById(R.id.textView3);

        num1.getText().toString();//Store this in a string say a
        num2.getText().toString();//Store this in a string say b

    /*you're actually the passing the literal "num1" or "num2". Something which you don't want.
    Instead, pass the strings a and b instead of num1 and num2 respectively*/
            int x =Integer.parseInt("num1");
            int y =Integer.parseInt("num2");

            int nas = x + y;

            tv.setText(nas);



            findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                /*You need to put the code here. i.e. You need to take the values from the
editTexts and store them in strings and finally integers when the button is clicked.*/
    }

        });
于 2013-04-24T06:14:01.733 回答
-1

尝试这个,

int x =Integer.valueof(""+num1.getText().toString());
int y =Integer.valueof(""+num2.getText().toString());

int nas = x + y;

tv.setText(""+String.valueof(nas));
于 2013-04-24T06:18:17.047 回答