2015-07-20 48 views
0

在配置活動內我有兩個控件調用相同的方法。這裏是方法:啓動意圖內部方法運行通過反射

public void goDetection() {  
     Intent detection = new Intent(this, Detection.class); 
     startActivity(detection); 
    } 

該方法是從一個基本的按鈕單擊執行,它工作正常。 同樣的方法也應該在AlertDialog內部執行,與「positiveButton」相關的方法。 我調用該方法的 「positiveButton」 的處理程序的onClick因爲這:

try { 
    Method method = Configuration.class.getMethod(methodPositive); 
    Object obj = new Configuration(); 
    method.invoke(obj); 
} catch (NoSuchMethodException e) { 
    Log.d("There is an error in Configuration class: NoSuchMethodException", e.getMessage()); 
} catch (IllegalAccessException e) { 
    Log.d("There is an error in Configuration class: IllegalAccessException", e.getMessage()); 
} catch (IllegalArgumentException e) { 
    Log.d("There is an error in Configuration class: IllegalArgumentException", e.getMessage()); 
} catch (InvocationTargetException e) { 
    Log.d("There is an error in Configuration class: InvocationTargetException", e.getMessage()); 
} 

其中methodPositive = 「goDetection」。 使用中我發現這個方法的獲取開始goDetection()方法Log.d()插入,但執行

startActivity(detection); 

語句時出現錯誤。從logcat的第一應用代碼相關的錯誤是:在上面的語句「NullPointerException異常的println需要一個消息」:

catch (InvocationTargetException e) { 
    Log.d("There is an error in Configuration class: InvocationTargetException", e.getMessage()); 
} 

這不是我第一次使用這個機制,但它是第一次我會被卡住。我錯過了什麼? 非常感謝!

+0

需要更多代碼。在某處打印或記錄未設置的輸入值。 – r2DoesInc

+0

我沒有打印任何東西,我記錄的唯一值是在捕獲部分,所有e.getMessage() –

+0

錯誤,「NullPointerException:println需要一條消息」 – r2DoesInc

回答

0

明白! 每個人都應該調用這個對象

Object obj = Configuration.this; 

,而不是我用

Object obj = new Configuration(); 

該系統得到了愚弄,因爲它不知道那裏的警告框被關聯到一個!