2015-05-06 67 views
-6

當我在類B中調用「getFirstKey」確定時, 但從A類程序崩潰。程序崩潰時調用另一個類的泛型函數

public class B { 

    public static Object getFirstKey(Map m) { 
     return = m.keySet().toArray()[0]; 
    } 

    public static void EnsureTest(Map m){ 
     Object myKey; 

     // print first key 
     myKey = getFirstKey(m); 
     System.out.println("Lowest key Stored in Map is.: " + myKey); 
    } 
} 

public class A { 
    private static Map<Long, YourDataClass> map; 

    public void print() { 
     Log.d("TEMP", "1"); 
     CreateTempMap(); 
     Log.d("TEMP", "2"); 
     System.out.println(map); 
     Log.d("TEMP", "3"); 
     B.EnsureTest(map); 
     Log.d("TEMP", "4"); 
     Object myKey; 
     myKey = B.getFirstKey(); 
     Log.d("TEMP", "5"); 
    } 
} 

我怎樣才能使安全調用從A類的方法?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.android.calendar/com.examples.android.calendar.CalendarView}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
      at android.app.ActivityThread.access$600(ActivityThread.java:156) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:153) 
      at android.app.ActivityThread.main(ActivityThread.java:5297) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.examples.android.calendar.B.getFirstKey(B.java:36) 

編輯:添加錯誤日誌,以使其更清晰!

+0

有一個堆棧跟蹤叫什麼名字? –

+0

發佈錯誤日誌。 –

+0

請顯示callstack,錯誤消息等。更多信息是有益的。 –

回答

2

您未通過Map,請致電,致電getFirstKey

你正在做正確的呼叫時,它被從B

相關問題