简体   繁体   中英

Error in running android app

i just started Android Studio and i am having error as i try to run my app.This is the error message i got:

Error:(53, 58) error: method getId in class View cannot be applied to given types;
required: no arguments
found: int
reason: actual and formal argument lists differ in length
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

My java code are below:

package com.ekesoft.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Layout
        RelativeLayout ekeLayout = new RelativeLayout(this);
        ekeLayout.setBackgroundColor(Color.BLUE);

        //Button
        Button redButton = new Button(this);
        redButton.setBackgroundColor(Color.GRAY);
        redButton.setText("Log in");

        //User name field
        EditText Username = new EditText(this);
        Username.setBackgroundColor(Color.WHITE);


        redButton.setId(2);
        Username.setId(3);


        //Creat a container for our layout
        RelativeLayout.LayoutParams ekedetail = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT

        );
        RelativeLayout.LayoutParams Userdetail = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT

        );

        //Set rules to position widget
        Userdetail.addRule(RelativeLayout.ABOVE,redButton.getId(2));
        Userdetail.addRule(RelativeLayout.CENTER_VERTICAL);
        Userdetail.setMargins(0,0,0,100);



        //Set the position of our container
        ekedetail.addRule(RelativeLayout.CENTER_HORIZONTAL);
        ekedetail.addRule(RelativeLayout.CENTER_VERTICAL);



        //Add widget to the layout, is now a child of the layout
        ekeLayout.addView(redButton,ekedetail);
        ekeLayout.addView(Username,Userdetail);

        //Set this as the content to be displayed
        setContentView(ekeLayout);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

My aim was to build simple interface using java.

the problem is this line

redButton.getId(2)

View.getId() doesn't take parameters. Here you can find the documentation. You have to remove the parameter. Eg

 redButton.getId()

In the message you have the answer:

Error:(53, 58)
error: method getId in class View cannot be applied to given types;
required: no arguments
found: int
reason: actual and formal argument lists differ in length
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.


Problem is method getId(int) is not defined in Android Button, but you can find getId()

So this line

Userdetail.addRule(RelativeLayout.ABOVE,redButton.getId(2));
//                                                      ↑ here you have an int!

Must be

Userdetail.addRule(RelativeLayout.ABOVE,redButton.getId());
//                                                      ↑ here!

NOTE: The getters methods of object's attributes usually does not accept any parameter.

You can do it like this

int id1=1;
int id2=2;

Then, write it as:

username.setId(id1);
redbutton.setId(id2);

Finally, don't pass any argument below in getId :

usernamedetails.addRule(RelativeLayout.ABOVE,redbutton.getId());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM