我有布局主要活动和另一个列表活动。但是,当我尝试呼叫列表活动时,我正在崩溃。请帮我开始列表活动(注意:列表活动有EditText和按钮,用于将文本添加到列表活动中的现有列表视图) 我只想添加listactivity中已经存在的文本列表视图。 非常感谢!!
公共类MainActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
EditText txt=(EditText)findViewById(R.id.txtPass);
if (txt.getText().length()==0){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
return;
}
if(txt.getText().toString().contains("Test")){
Button btn;
btn=(Button)findViewById(R.id.btnSubmit);
Intent intent=new Intent(this,Passwordlist.class);
startActivity(intent);
}
}
答案 0 :(得分:0)
首先,对代码进行一些更改:
Button btn;
EditText txt
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do this stuff here and not in onClick. Your views should be defined in //onCreate() and before the onCLick()
txt=(EditText)findViewById(R.id.txtPass);
btn=(Button)findViewById(R.id.btnSubmit);
}
public void onClick(View view){
Intent intent=new Intent(YourActivity.this,Passwordlist.class);
if (txt.getText().toString().matches("")){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
}
else if(txt.getText().toString().contains("Test")){
startActivity(intent);
}
}
看看是否有效。