gpt4 book ai didi

java - 如何调试 Android 应用程序强制关闭错误?

转载 作者:行者123 更新时间:2023-12-01 23:11:54 25 4
gpt4 key购买 nike

public class MainActivity extends Activity {

EditText oETextN;
EditText oETextPh;
Button oButtonSave;
public String givenName;
public String givenPhNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewsById();
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}

}

});

setContentView(R.layout.activity_main);

}

private void findViewsById(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);

//oEText = (EditText) findViewById(R.id.Name);

}

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

}

我一直在尝试制作一个简单的 Android 应用程序,将输入的详细信息(姓名和电话号码)直接保存到您的设备电话簿中。这是主要 Activity ,另一个类包含执行此操作的函数,我确信这些函数已正确实现。

当我在手机上运行该应用程序时,问题就出现了,不幸的是,经过几个小时的冷编码后,我收到了强制关闭错误!

最佳答案

更改为

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();

然后

  private  void initializeViews(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
//oEText = (EditText) findViewById(R.id.Name);
}

您需要先将布局的内容设置到 Activity ,然后初始化 View 。

还将 findViewsById(); 重命名为 initializeViews() 或更合适的名称,因为您可以使用 findViewById 作为 oButtonSave 初始化 View =(按钮)findViewById(R.id.SaveBtn);

编辑:

在 View 初始化后具有点击监听器。

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}

}

});
}

关于java - 如何调试 Android 应用程序强制关闭错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21807686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com