2

我是 Rest Template 的新手,当我从 MainActivity 调用 @background 以从 rest 服务器检索数据时,这很好。但是在我从 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 时,我遇到了一个奇怪的问题。这是我的 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)

我花了几个小时来解决这个问题,但我是 rest 模板的新用户,我也不太了解 rest 模板的工作原理。我希望有人能让我摆脱这个问题。

谢谢,

4

2 回答 2

0
   RestClient restClientFragment;

String str=restClientFragment.main().toString();

你应该在某个地方初始化restClientFragment!它为空,因此您的 NE 错误。

于 2013-06-17T05:19:34.753 回答
0

我有办法解决调用接口方法表单片段的NullPointerException这个问题,我不确定这个问题可能与片段的后台线程有关。片段是主要活动的子活动,正如我在上面的意思,我可以成功地从主要活动中调用接口方法。我将 RestClient 接口声明为“RestClient restClient;” 在片段(子活动)中,但是当我访问“restClient.main()”时,我得到了一个空指针异常。我自己不清楚这个问题,这是我解决这个问题的代码。

我附加了名为 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();

}

于 2013-06-18T08:27:57.643 回答