2013-02-14 33 views
2

我是Android和JUnit測試的新手。 我試圖想出一些測試用例爲MyFirstApp例如如下解釋: http://developer.android.com/training/basics/firstapp/index.htmlrunOnUiThread和sendKeys調用的序列,導致Android上的NPE

這裏是我的測試類看起來像:

@Override 
protected void setUp() throws Exception { 
    // TODO Auto-generated method stub 
    super.setUp(); 
    setActivityInitialTouchMode(false); 

    mainActivity = (MainActivity)getActivity(); 

    editText = (EditText) mainActivity.findViewById(
          com.example.myfirstapp.R.id.edit_message); 
    button = (Button) mainActivity.findViewById(
          com.example.myfirstapp.R.id.button1); 

} 

public void testPreconditions(){ 
    assertTrue(editText.getHint().toString().equals(
        mainActivity.getString(
         com.example.myfirstapp.R.string.edit_message))); 
} 


public void testUI(){ 
    mainActivity.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      editText.performClick(); 

     } 
    }); 
    getInstrumentation().waitForIdleSync(); 

    this.sendKeys(KeyEvent.KEYCODE_A); 
    this.sendKeys(KeyEvent.KEYCODE_B); 
    this.sendKeys(KeyEvent.KEYCODE_C); 
    this.sendKeys(KeyEvent.KEYCODE_D); 
    this.sendKeys(KeyEvent.KEYCODE_E); 



    mainActivity.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      button.performClick(); 

     } 
    }); 
} 

的testPrecontions測試成功。然而,這給NPE「button.performClick();」

有人能指出我可能做錯了什麼嗎?

感謝 -Angshu

回答

2

好吧,我想我理解了它那裏是需要添加了調用getInstrumentation().waitForIdleSync()第二runOnUiThread通話後如下面的

public void testUI(){ 

    mainActivity.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      editText.performClick(); 

     } 
    }); 

    this.sendKeys(KeyEvent.KEYCODE_A); 
    this.sendKeys(KeyEvent.KEYCODE_B); 
    this.sendKeys(KeyEvent.KEYCODE_C); 
    this.sendKeys(KeyEvent.KEYCODE_D); 
    this.sendKeys(KeyEvent.KEYCODE_E); 



    mainActivity.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      button.performClick(); 

     } 
    }); 
    getInstrumentation().waitForIdleSync(); 
} 

希望這有助於其他人被困在一個類似的情況。

謝謝大家! -Ashshu

+0

這將是我的下一個消化,你也可以接受你自己的答案來表明,這個問題是成功的答案。 – Simulant 2013-02-17 14:29:14

0

按鈕沒有分配的ID,所以#findViewByID回報null,你不能在null-Object調用performClick()

這應該是你的GUI聲明(從教程中複製),對吧?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" /> 
</LinearLayout> 

在ID屬性添加的按鈕,像由EditText上,這樣就可以解決的查看查找錯誤:

<Button android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_send" /> 
+0

我確實在xml文件中有按鈕的id。這有點奇怪,因爲如果我在第一次調用runOnUiThread時按下了按鈕,則測試成功,但它似乎只會在連續調用runOnUiThread時失敗。我的意思是,同時在testUI button.performClick()上面的失敗,以下運行成功: '公共無效testUI(){ \t \t mainActivity.runOnUiThread(新的Runnable(){ \t \t \t \t \t \t @覆蓋 \t \t \t公共無效的run(){ \t \t \t \t editText.performClick(); \t \t \t \t button.performClick(); \t \t \t \t \t \t \t} \t \t}); \t \t getInstrumentation()。waitForIdleSync(); \t \t \t}' 我做錯了什麼?請幫助 – Angshu 2013-02-17 09:16:49

相關問題