2012-05-18 62 views
0

我想刷新我的自定義ListViewAdapter;LayoutInflater空指針異常

編輯:

當我ListView中單擊一個項目它調用GoruntuleActivity的屏幕和創建onCreate方法(我假設它正在創建..)然後我打電話GoruntuleActivity和的方法layputInflater進入這個Activity。

ListeleActivity.java

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View item, 
        int position, long id) { 

       onClickPosition = position; 

       final Intent intent = new Intent(ListeleActivity.this, 
         GoruntuleActivity.class); 
       startActivity(intent); 

       goruntuleNesnesi.isSetListRefresh(onClickPosition , issetlist); 
      } 
     }); 

並呼籲GoruntuleActivity.java

public void isSetListRefresh(int onClickPosition, ArrayList<String> issetlist) 
    { 
     issetlist.set(onClickPosition, "false"); 
     setRecentOrSeen(issetlist); 
     ListviewRefresh(); 
    } 

    public void ListviewRefresh() 
    { 
     LayoutInflater mLInflater = LayoutInflater.from(this); 

     ListViewAdapter adapter = new ListViewAdapter(
       getApplicationContext(), getKimdenList(), getKonuList(), getRecentOrSeen() , 
       mLInflater); 
     adapter.Remove(); 

    } 

的方法,而現在它給我另一個例外..

05-18 16:25:22.073: E/AndroidRuntime(807): FATAL EXCEPTION: main 
05-18 16:25:22.073: E/AndroidRuntime(807): java.lang.IllegalStateException: System services not available to Activities before onCreate() 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.app.Activity.getSystemService(Activity.java:3526) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.view.LayoutInflater.from(LayoutInflater.java:171) 
05-18 16:25:22.073: E/AndroidRuntime(807): at com.mobil.eposta.GoruntuleActivity.ListviewRefresh(GoruntuleActivity.java:160) 
05-18 16:25:22.073: E/AndroidRuntime(807): at com.mobil.eposta.GoruntuleActivity.isSetListRefresh(GoruntuleActivity.java:155) 
05-18 16:25:22.073: E/AndroidRuntime(807): at com.mobil.eposta.ListeleActivity$1.onItemClick(ListeleActivity.java:137) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.widget.ListView.performItemClick(ListView.java:3382) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.os.Handler.handleCallback(Handler.java:587) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.os.Looper.loop(Looper.java:123) 
05-18 16:25:22.073: E/AndroidRuntime(807): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-18 16:25:22.073: E/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method) 
05-18 16:25:22.073: E/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:521) 
05-18 16:25:22.073: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-18 16:25:22.073: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-18 16:25:22.073: E/AndroidRuntime(807): at dalvik.system.NativeStart.main(Native Method) 
+0

您試過使用LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);? –

+0

我現在用它,它沒有工作 – Merve

+0

LayoutInflater.from(getApplicationContext())? – Blackbelt

回答

2

從錯誤日誌中,它看起來像getApplicationContext()返回null。你什麼時候打電話goruntuleNesnesi.ListviewRefresh();?它在onCreate(...)中嗎?如果是這樣,那可能是你的問題,因爲getApplicationContext(...)將返回null,直到onCreate(...)完成初始化。

編輯:我查看了一下堆棧跟蹤,看起來好像你在點擊按鈕時調用ListviewRefresh。您是否嘗試過使用this而不是getApplicationContext(...)?

編輯2:試試這個:

ListViewAdapter adapter = new ListViewAdapter(this, getKimdenList(), getKonuList(), getRecentOrSeen() ,mLInflater); 

雖然,我認爲這可能有同樣的問題,因爲該活動尚未在屏幕上繪製但並沒有被初始化,你不能誇大其意。試試看,我們會從那裏開始。

+0

你是對的!我在未創建oncrete時調用layoutinflater。我嘗試在goruntuleactivity的創建之後打電話給我,但它給了我一個「真正的錯誤」。我的意思是在你的評論之前我的應用程序給予橙色的例外,並沒有崩潰,但在oncreate和layoutinflater調用它後崩潰:S我該怎麼辦?我在我的問題編輯我的新的Logcat錯誤 – Merve

+0

我試過這個,結果讓我感到困惑:SI會用簡單模式編輯我所有的代碼來解釋我的代碼.... – Merve

+0

請你再看看我的問題.. – Merve