我在MainActivity.java得到错误无缘无故 [英] I get errors in my MainActivity.java for no reason

查看:807
本文介绍了我在MainActivity.java得到错误无缘无故的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以使eclpse为Android计算器和我得到的所有这些错误没有理由,这是我的MainActivity

I am "trying" to make a calculator in eclpse for android and i get all these errors for no reason this is my 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

另外检查是否有任何更多的错误,我会看到当我运行应用程序在此先感谢! :) 编辑看到别人对我的最后一个问题还纠正了code感谢@kathir 编辑好解决的问题谢谢大家

Also check if it has any more mistakes that i will see when i run the app thanks in advance! :) EDIT see on else for my last problem also corrected the code thanks to @kathir EDIT OK ISSUE RESOLVED THANKS EVERYBODY

推荐答案

这是你的code应该如何,

This is how your code should be,

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

这篇关于我在MainActivity.java得到错误无缘无故的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆