2013-02-20 56 views
3

我在這一行開空指針異常wsresult = restClientInterface.skuska();用於restClientInterface。下面是我的示例:Android @RestService注入通過androidannotations結果到nullPointerException異常

import org.springframework.http.converter.json.GsonHttpMessageConverter; 

import com.googlecode.androidannotations.annotations.rest.Get; 
import com.googlecode.androidannotations.annotations.rest.Rest; 

@Rest(rootUrl = "http://my_ip_address/webresources", converters = {GsonHttpMessageConverter.class}) 
public interface RestClient { 

    @Get("/persons") 
    String skuska();  
} 

,我使用它分段

@EFragment 
public class HomeFragment extends Fragment { 

private Button ws; 
private TextView wsstring; 
private String wsresult;   

     @RestService 
     RestClient restClientInterface; 

     wsstring = (TextView) view.findViewById(R.id.wsstring); 

       ws = (Button) view.findViewById(R.id.ws); 
       ws.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         getWSResult(); 
         wsstring.setText(wsresult);   
        } 
       }); 

       return view; 
      } 

      @Background 
      public void getWSResult() { 
       wsresult = restClientInterface.skuska();  
      } 
+0

是嗎? restClientInterface顯然是空的,你可能想要像初始化它? – njzk2 2013-02-20 11:28:00

+0

你能發佈異常的完整堆棧跟蹤嗎? – 2013-02-21 09:55:59

回答

2

你的RESTClient實現應該通過正確AA只是你的片段後,被注入已準備就緒。 你可以複製/粘貼生成的類,看看發生了什麼?

此外,您打電話給getWSResult()(這是一種背景方法),並在你的TextView中設置Rest調用的結果之後。但是,在你試圖在對象中設置結果之前,你不能說結果是否已經到達。

你應該嘗試用類似這樣的代碼:

@EFragment(R.layout.homeFragment) 
public class homeFragment extends Fragment { 

    @ViewById 
    Button ws; 

    @ViewById 
    TextView wsstring; 

    @RestService 
    RestClient restClientInterface; 

    private String wsresult; 

    @Click 
    void wsstringClicked() { 
     getWSResult(); 
    } 

    @Background 
    void getWSResult() { 
     wsresult = restClientInterface.skuska(); 
    } 

    @UiThread 
    void updateUI() { 
     wsstring.setText(wsresult); 
    } 
} 

編輯:只是想着它:剛上@ViewById註釋字段

EDIT2去除private。你怎麼在你的活動中使用這個片段? 您使用的是@FragmentById還是@FragmentByTag

+0

這是爲片段http://paste2.org/p/2921505 – 2013-02-20 13:44:13

+0

而生成的,而我的活動中的片段就像​​這樣使用http://paste2.org/p/2921565 – 2013-02-20 13:57:42

+0

您可以在第17行放置斷點或登錄生成的片段,並告訴我,如果你有什麼?如果這個方法沒有被調用,你可能已經發現了一個AA的錯誤,我邀請你在[github]上報告問題(https://github.com/excilys/androidannotations/issues?state=open) – DayS 2013-02-21 19:11:50

相關問題