我没有理由在MainActivity.java中收到错误

时间:2014-01-03 12:18:27

标签: java android eclipse

我正在“尝试”在eclpse中为Android制作一个计算器,我无缘无故地得到所有这些错误这是我的MainActivity

package com.odysseus.calculator;

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

public class MainActivity extends Activity {

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

    if(num1text.getText().toString().isEmpty()  || num2text.getText().toString().isEmpty())
    {
        Toast msg = Toast.makeText(getBaseContext(), "You Should Put Numbers To Do Calculation", Toast.LENGTH_LONG);
        msg.show();
    }
//EDIT ok last problem is here with else underlines { and if add another one on the bottom says delete this token...
else
{
public void calcadd(View v){ //here
        EditText number1text=(EditText)findViewById(R.id.num1text);
        EditText number2text=(EditText)findViewById(R.id.num2text);
        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text+num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcaminus(View v){ //here
        EditText number1text=(EditText)findViewById(R.id.num1text);
        EditText number2text=(EditText)findViewById(R.id.num2text);
        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text-num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcadivide(View v){ //here
        EditText number1text=(EditText)findViewById(R.id.num1text);
        EditText number2text=(EditText)findViewById(R.id.num2text);
        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text/num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcmultiply(View v){ //here
        EditText number1text=(EditText)findViewById(R.id.num1text);
        EditText number2text=(EditText)findViewById(R.id.num2text);
        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text*num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

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

}

这些是问题:

Syntax error on token "(", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 37 Java Problem
Syntax error on token ")", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 27 Java Problem
Syntax error on token "(", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 47 Java Problem
Syntax error on token ")", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 37 Java Problem
Syntax error on token "(", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 27 Java Problem
Syntax error on token ")", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 47 Java Problem
Syntax error on token "(", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 57 Java Problem
Syntax error on token ")", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 57 Java Problem
Syntax error on token "(", ; expected   MainActivity.java   /Calculator/src/com/odysseus/calculator line 68 Java Problem

另外,请检查我在运行应用程序时是否会有更多错误提前感谢! :) 编辑在其他地方看到我的最后一个问题也纠正了代码感谢@kathir 编辑好的问题解决了感谢每一个人

5 个答案:

答案 0 :(得分:2)

标记为

} //here

您刚刚“关闭”onCreate(),因此以下所有代码均无效。您需要删除该括号。它也必须是else,而不是Else

答案 1 :(得分:1)

你的if if是否在任何方法之外。

Else应为else

您的EditText number1text=(EditText)findViewById(R.id.num1text);已在其他部分初始化。

您需要在onCreate

中初始化您的观看次数
EditText number1text,number2text;
TextView answer;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    number1text=(EditText)findViewById(R.id.num1text);
    number2text=(EditText)findViewById(R.id.num2text);
    answer=(TextView)findViewById(R.id.ans); 
} 
 public void calcaminus(View v){ 
  if(num1text.getText().toString().isEmpty()  || num2text.getText().toString().isEmpty())
     {
      // your toast message
     }
    else
    {
    Integer num1text=Integer.parseInt(number1text.getText().toString());
    Integer num2text=Integer.parseInt(number2text.getText().toString());
    Integer ans=num1text+num2text;
    answer.setText("Answer:"+ans.toString());
    }
} 
...// rest of the methods.

无需每次都在点击时初始化您的观看次数。在onCreate中初始化一次。

答案 2 :(得分:1)

  1. 您已关闭onCreate而未包含if-else 也就是说,在这一行:

    }//here
    
  2. Else应为小型上限,即:

    else
    {
    
    }
    

答案 3 :(得分:0)

package com.odysseus.calculator;

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

public class MainActivity extends Activity {

EditText number1text;
EditText number2text;

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

        number1text=(EditText)findViewById(R.id.num1text);
        number2text=(EditText)findViewById(R.id.num2text);


    if(num1text.getText().toString().isEmpty()  || num2text.getText().toString().isEmpty())
    {
        Toast msg = Toast.makeText(getBaseContext(), "You Should Put Numbers To Do Calculation", Toast.LENGTH_LONG);
        msg.show();
    }
//if i put here else it show an error again help me on that too
else
{
public void calcadd(View v){ //here

        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text+num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcaminus(View v){ //here

        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text-num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcadivide(View v){ //here

        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text/num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

    public void calcmultiply(View v){ //here

        Integer num1text=Integer.parseInt(number1text.getText().toString());Integer num2text=Integer.parseInt(number2text.getText().toString());
        Integer ans=num1text*num2text;

        TextView answer=(TextView)findViewById(R.id.ans);
        answer.setText("Answer:"+ans.toString());
    }

} //here



}

尝试一下dude:)

答案 4 :(得分:0)

这就是你的代码应该如何,

public class MainActivity extends Activity {

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

        if (num1text.getText().toString().isEmpty()
                || num2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
                    "You Should Put Numbers To Do Calculation",
                    Toast.LENGTH_LONG);
            msg.show();
        } else {
        }
    }

    public void calcadd(View v) { // here
        EditText number1text = (EditText) findViewById(R.id.num1text);
        EditText number2text = (EditText) findViewById(R.id.num2text);
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text + num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcaminus(View v) { // here
        EditText number1text = (EditText) findViewById(R.id.num1text);
        EditText number2text = (EditText) findViewById(R.id.num2text);
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text - num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcadivide(View v) { // here
        EditText number1text = (EditText) findViewById(R.id.num1text);
        EditText number2text = (EditText) findViewById(R.id.num2text);
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text / num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcmultiply(View v) { // here
        EditText number1text = (EditText) findViewById(R.id.num1text);
        EditText number2text = (EditText) findViewById(R.id.num2text);
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text * num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

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