我是Rest模板中的新手,当我从MainActivity调用@background从rest服务器检索数据时,它很好。但是在我从片段中调用它之后,它就没有了。
这是我的主要内容:
package com.example.androidkickstart;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.example.androidkickstart.adapter.ViewPagerAdapter;
import com.example.androidkickstart.object.MoviesList;
import com.example.androidkickstart.rest.RestClient;
import com.example.androidkickstart.rest.getMovieListAsync;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.rest.RestService;
@EActivity(R.layout.activity_main)
public class MainActivity
extends SherlockFragmentActivity
implements OnPageChangeListener, TabListener
{
/******* Declare Class *******/
@RestService
RestClient restClient;
/******* Variable Declaration *******/
private String[] locations;
MoviesList movieList;
/******* View Declaration *******/
@ViewById
ViewPager pager;
ListView List;
/********** Things to do after View Declaration *********/
@AfterViews
void afterViews() {
locations = getResources().getStringArray(R.array.locations);
configureViewPager();
configureActionBar();
doSomethingInBackground();
}
/******* samilar to OnPostExecute, OnPreExecute *******/
@UiThread
void doSomethingElseOnUiThread() {
// do something on UIThread
}
@Background
void doSomethingInBackground() {
movieList=restClient.main();
doSomethingElseOnUiThread();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater();
return true;
}
private void configureViewPager() {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), locations);
pager.setAdapter(viewPagerAdapter);
pager.setOnPageChangeListener(this);
}
public void onPageSelected(int position) {
Tab tab = getSupportActionBar().getTabAt(position);
getSupportActionBar().selectTab(tab);
}
private void configureActionBar() {
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String location: locations) {
Tab tab = getSupportActionBar().newTab();
tab.setText(location);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = tab.getPosition();
pager.setCurrentItem(position);
}
@Override
public void onPageScrollStateChanged(int position) {}@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}
这是我的RestClient:
package com.example.androidkickstart.rest;
import com.example.androidkickstart.object.MoviesList;
import com.googlecode.androidannotations.annotations.rest.Get;
import com.googlecode.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;;
@Rest(rootUrl = "http://mubi-app.herokuapp.com", converters = MappingJacksonHttpMessageConverter.class)
public interface RestClient {
@Get("/api/v1/home")
public MoviesList main();
}
但是当我从MovieListFragment(Fragment)调用我的RestClient时,我遇到了一个奇怪的问题。 这是我的MovieListFragment ......
package com.example.androidkickstart;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.example.androidkickstart.adapter.BaseAdapter;
import com.example.androidkickstart.object.MoviesList;
import com.example.androidkickstart.object.movies;
import com.example.androidkickstart.rest.RestClient;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.rest.RestService;
@EFragment(R.layout.layout_main)
public class MovieListFragment
extends Fragment
{
View v;
MoviesList moviesList;
BaseAdapter adapter;
Context context;
@RestService
RestClient restClientFragment;
@ViewById
TextView txtMovieList;
ListView list;
@AfterViews
void afterViews() {
txtMovieList.setText("Movie LIst");
doSomethingInBackground();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
v=inflater.inflate(R.layout.layout_main, null);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
context=view.getContext();
txtMovieList=(TextView)v.findViewById(R.id.txtMovieList);
list=(ListView)v.findViewById(R.id.list);
afterViews();
}
/******* samilar to OnPostExecute, OnPreExecute *******/
@UiThread
void doSomethingElseOnUiThread() {
// do something on UIThread
}
@Background
void doSomethingInBackground() {
String str=restClientFragment.main().toString();
doSomethingElseOnUiThread();
}
}
这是我的错误......
06-17 11:17:07.919: E/AndroidRuntime(791): FATAL EXCEPTION: main
06-17 11:17:07.919: E/AndroidRuntime(791): java.lang.NullPointerException
06-17 11:17:07.919: E/AndroidRuntime(791): at com.example.androidkickstart.MovieListFragment.doSomethingInBackground(MovieListFragment.java:88)
06-17 11:17:07.919: E/AndroidRuntime(791): at com.example.androidkickstart.MovieListFragment.afterViews(MovieListFragment.java:62)
06-17 11:17:07.919: E/AndroidRuntime(791): at com.example.androidkickstart.MovieListFragment.onViewCreated(MovieListFragment.java:76)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.View.measure(View.java:8313)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.view.ViewRoot.handleMessage(ViewRoot.java:1863)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:130)
06-17 11:17:07.919: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:3768)
06-17 11:17:07.919: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 11:17:07.919: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 11:17:07.919: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-17 11:17:07.919: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-17 11:17:07.919: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)
我花了几个小时来解决这个问题,但我是休息模板的新用户,而且我也不太了解休息模板是如何工作的。我希望有人能让我摆脱这个问题。
谢谢,
答案 0 :(得分:0)
RestClient restClientFragment;
String str=restClientFragment.main().toString();
您应该在某处初始化restClientFragment
!它是null因此你的NE错误。
答案 1 :(得分:0)
我有办法解决调用接口方法表单片段的NullPointerException这个问题,我不确定该问题可能与片段的后台线程有关。而片段是主要活动的子活动,我的意思是在上面我成功地可以从主要活动调用接口方法。我将RestClient接口声明为“RestClient restClient;”在片段(subActivity)但是当我访问“restClient.main()”时,我得到了一个nullpointerexception。我不清楚自己这个问题在这里是我的代码我解决了这个问题。
我追加名为RestClientHelper.java的类
public class RestClientHelper{
static RestClient restClient;
public void setCallRestClient(RestClient rs){
restClient=rs;
}
public MoviesList CallRestClient(){
return restClient.main();
}
}
“静态RestClient restClient;”在那一行我使用静态来保存RestClient()的指针。 我将MainActivity.java的RestClient传递给RestClientHelper.java并分配给restClient。我将这行代码附加到MainActivity.java。
@AfterViews
void afterViews() {
locations = getResources().getStringArray(R.array.locations);
configureViewPager();
configureActionBar();
doSomethingInBackground();
restHelper=new RestClientHelper();
restHelper.setCallRestClient(restClient);
}
我改变了我的MovieListFragment.java代码,如下所示;
@Background
void doSomethingInBackground() {
String str=restClientFragment.main().toString();
doSomethingElseOnUiThread();
RestClientHelper rsHelper=new RestClientHelper();
moviesList=rsHelper.CallRestClient();
}