[英]Android Studio App crashes on run after successful build and apk installation
This is the simple java code used by me on login screen. 这是我在登录屏幕上使用的简单Java代码。 App closes itself after the splash screen which I applied. 应用程序在我启动的初始屏幕后关闭。 It doesnt go any further. 它没有任何进一步。 what to do? 该怎么办?
public class MainActivity extends AppCompatActivity {
EditText username = (EditText)findViewById(R.id.input_email);
EditText password = (EditText)findViewById(R.id.input_password);
Button loginButton = (Button) findViewById(R.id.btn_login);
int counter=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
//Login Button Code
public void onBtn(View v){
if(username.getText().toString().equals("shubham.goyal1210@gmail.com")&&
(password.getText().toString().equals("shubham")))
{
Toast.makeText(this, "Login Success", Toast.LENGTH_LONG).show();}
else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show();
counter--;
if(counter==0){ loginButton.setEnabled(false); }
else
{
Toast.makeText(this, counter+" attempts left", Toast.LENGTH_SHORT).show(); }
}}
//Sign Up Button Code
public void linkSign(View v){
Intent intent = new Intent(this, signupActivity.class);
startActivity(intent);
}}
move 移动
EditText username = (EditText)findViewById(R.id.input_email);
EditText password = (EditText)findViewById(R.id.input_password);
Button loginButton = (Button) findViewById(R.id.btn_login);
after setContentView
while keeping reference variable outside oncreate
because views are only available in the UI hierarchy of the activity after invocation of setContentView
在setContentView
之后,同时将引用变量保留在oncreate
之外,因为视图仅在调用setContentView
后在活动的UI层次结构中可用
EditText username;
EditText password;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.input_email);
password = (EditText)findViewById(R.id.input_password);
loginButton = (Button) findViewById(R.id.btn_login);
}
You initialize EditText
and Button
view on wrong place ,you need to initialize it inside onCreate
method like below. 您在错误的地方初始化EditText
和Button
视图,您需要在如下的onCreate
方法中对其进行初始化。
public class MyActivity extends AppCompatActivity {
EditText username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.input_email);
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.