尝试更新TextView

时间:2016-02-04 10:39:48

标签: java android android-fragments

我有一个包含TabLayout片段的活动。创建活动时,它会进行网络调用并接收POJO,我用它来设置图像和工具栏标题。完成后,我将Object发送到TabLayout中的一个片段,这样我就可以操作Tabs中的数据了。我遇到的问题是,当片段获取对象时,由于某种原因,它会因Nullpointer而崩溃。

我认为是因为创建了TextView,然后网络调用完成,然后我尝试使用POJO中的数据在TextView上设置文本。

片段:

public class OverviewListItemFragment extends Fragment implements OverviewView {

  TextView overViewHeading;

  public OverviewListItemFragment() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
overViewHeading = (TextView) getActivity().findViewById(R.id.frag_overview_heading_textview);
return inflater.inflate(R.layout.overview_list_item_fragment, container, false);

}

@Override
public void populateOverviewViews(TradeItem aTradeItem) {
String title;
title = aTradeItem.getItem().getTradeTitle();

overViewHeading.setText(title);
System.out.println("Overview Trade Object title is: " + title);
}

/**
 * Called when a fragment is first attached to its context.
 * {@link #onCreate(Bundle)} will be called after this.
 *
 * @param context
 */
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}

的活动:

public class ItemBuyNowActivity extends AppCompatActivity implements ItemView {

  private ImageLoader mImageLoader = VolleySingleton.getInstance().getImageLoader();

  @Bind(R.id.coordinatorLayout_list_item)
  CoordinatorLayout mCoordinatorLayout;

  @Bind(R.id.collapsing_toolbar_buy_now_list_item)
  CollapsingToolbarLayout buyNowCollapsingToolbar;
  @Bind(R.id.buy_now_tool_bar)
  Toolbar toolbar;
  @Bind(R.id.buy_now_tabs)
  TabLayout buy_now_tabs;
  ViewPager buy_now_viewpager;

  private ItemPresenter presenter;
  private OverviewPresenter mOverviewPresenter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_buy_now);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);

    buy_now_viewpager = (ViewPager) findViewById(R.id.buy_now_viewpager);
    setupViewPager(buy_now_viewpager);
    buy_now_tabs.setupWithViewPager(buy_now_viewpager);
    handleIntent(getIntent());

  }

  private void setupViewPager(ViewPager aBuy_now_viewpager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OverviewListItemFragment(), "Overview");
    adapter.addFragment(new ShippingListItemFragment(), "Shipping");
    adapter.addFragment(new DescriptionListItemFragment(), "Description");
    adapter.addFragment(new PaymentListItemFragment(), "Payment");
    aBuy_now_viewpager.setAdapter(adapter);

  }

  private void handleIntent(Intent aIntent) {
    if (aIntent != null) {

      String tradeType = aIntent.getStringExtra("itemType");
      String tradeId = aIntent.getStringExtra("itemId");
      tradeId = "215409903";

      presenter = new ItemPresenterImpl(this, ItemBuyNowActivity.this);
      presenter.doListingServiceCall(tradeId);


    } else {
      System.out.println("Intent is null in " + ItemBuyNowActivity.class.getSimpleName());
    }

  }

  @Override
  public void populateListViews(TradeItem aTradeItem) {
    populateOverview(aTradeItem);
  }

  private void populateOverview(TradeItem aTradeItem){
    mOverviewPresenter = new OverviewPresenterImpl(new OverviewListItemFragment(), aTradeItem);
  }
  @Override
  public void loadBackgroundImage(Drawable aDrawable) {
    buyNowCollapsingToolbar.setBackground(aDrawable);

  }

  @Override
  public void displayErrorMessage(String errorMessage) {

  }



  class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
      super(manager);
    }

    @Override
    public Fragment getItem(int position) {
      return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
      return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
      mFragmentList.add(fragment);
      mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return mFragmentTitleList.get(position);
    }
  }


}

logcat的:

02-03 20:10:23.175 29085-29085/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                              java.lang.NullPointerException
                                                                  at com.example.app.ui.fragments.OverviewListItemFragment.populateOverviewViews(OverviewListItemFragment.java:41)
                                                                  at com.example.app.listing.overview.OverviewPresenterImpl.<init>(OverviewPresenterImpl.java:14)
                                                                  at com.example.app.ui.activities.ItemBuyNowActivity.populateOverview(ItemBuyNowActivity.java:104)
                                                                  at com.example.app.ui.activities.ItemBuyNowActivity.populateListViews(ItemBuyNowActivity.java:100)
                                                                  at com.example.app.listing.ItemPresenterImpl.onResponse(ItemPresenterImpl.java:62)
                                                                  at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
                                                                  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                  at android.os.Handler.handleCallback(Handler.java:725)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                  at android.os.Looper.loop(Looper.java:137)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                  at dalvik.system.NativeStart.main(Native Method)

如何解决这个问题,以免它崩溃?

由于

1 个答案:

答案 0 :(得分:0)

您的问题发生在OverviewListItemFragment.populateOverviewViews中,这让我相信参数为null或视图尚未创建。 你在populateOverview之前打电话给onViewCreated吗?