我是休息模板的新手,当我从 MainActivity 调用 @background 从休息服务器检索数据时,这很好。但是在我从 Fragment 调用它之后,它并没有醒来。
这是我的主要:
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时,我遇到了一个奇怪的问题。这是我的电影列表片段...
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)
我花了几个小时来解决这个问题,但我是休息模板的新用户,而且我对休息模板的工作原理也不是很好。我希望有人能让我摆脱这个问题。
谢谢
RestClient restClientFragment;
String str=restClientFragment.main().toString();
您应该在某处初始化restClientFragment
!它是空的,因此您的NE错误。
我有一种方法可以解决调用接口方法表单片段的 NullPointerException 问题,我不确定问题是否可能与片段的背景线程有关。片段是主活动的子活动,正如我在上面的意思,我成功地从主活动调用接口方法。我在片段(子活动(中将 RestClient 接口声明为 "RestClient restClient;",但是当我访问 "restClient.main((" 时,我得到了一个 nullpointerexception。我自己不清楚这个问题,这是我的代码,我解决了这个问题。
我附加了名为 RestClientHelper 的类.java
public class RestClientHelper{
static RestClient restClient;
public void setCallRestClient(RestClient rs){
restClient=rs;
}
public MoviesList CallRestClient(){
return restClient.main();
}
}
"static RestClient restClient;" 在这一行中,我使用 static 来保存 RestClient(( 的指针。我将 MainActivity 的 RestClient 传递给 RestClientHelper.java.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();
}