2014-02-16 234 views
0
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應用程序,將輸入的詳細信息(姓名和電話號碼)直接保存到您的設備電話簿中。這是MAIN活動,另一個類包含執行此操作的功能,我相信這是正確實施的。如何調試Android應用程序強制關閉錯誤?

當我在手機上運行應用程序時出現問題,而且經過幾個小時的冷碼後,我得到了FORCE CLOSE錯誤!

+0

小時?你的日誌發生了什麼? –

+0

'findViewsById'應該在'setContentView'後面調用 –

回答

3

更改爲

@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); 
    } 

您需要首先設置佈局的內容到活動,則初始化意見。

也改名findViewsById();initializeViews()或更合適些因爲你在findViewById作爲oButtonSave = (Button) findViewById(R.id.SaveBtn);

編輯初始化您的觀點:

有點擊收聽的視圖初始化後。

@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"); 
     } 

    } 

}); 
} 
+0

我要問的內容可能是模糊的,但是由於我的Android經驗不足,我應該在哪裏放置onclicklistener方法呢? – user3050101

+0

@ user3050101在方法 – Raghunandan

+0

內部初始化視圖之後,請更具體一些,這可以在第二秒鐘內解決我的問題(: – user3050101