[英]Error in running android app
我刚刚启动了Android Studio
,但在尝试运行我的应用程序时出现错误。这是我收到的错误消息:
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'.
编译失败; 有关详细信息,请参阅编译器错误输出。
我的java代码如下:
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);
}
}
我的目标是使用 java 构建简单的界面。
在消息中,您有答案:
错误:(53, 58)
错误:类 View 中的方法 getId无法应用于给定类型;
要求:无参数
找到:int
原因:实际和形式参数列表的长度不同
错误:任务 ':app:compileDebugJavaWithJavac' 的执行失败。
问题是方法getId(int)
未在 Android Button 中定义,但您可以找到getId()
所以这一行
Userdetail.addRule(RelativeLayout.ABOVE,redButton.getId(2));
// ↑ here you have an int!
必须是
Userdetail.addRule(RelativeLayout.ABOVE,redButton.getId());
// ↑ here!
注意:对象属性的 getters 方法通常不接受任何参数。
你可以这样做
int id1=1;
int id2=2;
然后,将其写为:
username.setId(id1);
redbutton.setId(id2);
最后,不要在getId
传递以下任何参数:
usernamedetails.addRule(RelativeLayout.ABOVE,redbutton.getId());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.