如何在Android中设置图像

时间:2016-11-13 11:14:02

标签: android android-imageview android-drawable android-image

我想以编程方式设置image,我应该将图片传递给构造函数。我使用此代码但在运行应用程序时显示强制关闭

强制关闭错误:

FATAL EXCEPTION: main
                  Process: ir.mototel.mototel, PID: 12904
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                      at com.sepandar.xengine.fragment.HomeFragment.newInstance(HomeFragment.java:60)
                      at com.sepandar.xengine.adapter.HomePagerAdapter.getItem(HomePagerAdapter.java:43)
                      at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:101)
                      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1038)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1186)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                      at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:544)
                      at com.sepandar.xengine.fragment.MainFragment$1.onResponse(MainFragment.java:174)
                      at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5349)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

在片段中初始化imageView setImage

public class HomeFragment extends BaseFragment {
private static final String TAG = "HomeFragment";
public int categoy;
@Bind(R.id.recyclerView)
RecyclerView recyclerView;
@Bind(R.id.testFragmentImageID)
ImageView fragmentImage;
private HomeAdapter homeAdapter;
private Gson gson;
private ArrayList<Content> slider = new ArrayList<>();
private ArrayList<TopChart> topCharts = new ArrayList<>();


public static HomeFragment newInstance(int position, ArrayList<TopChart> topChartsArray, int imageINT) {
    Bundle bundle = new Bundle();
    HomeFragment instance = new HomeFragment();
    instance.categoy = position;
    instance.topCharts.addAll(topChartsArray);
    instance.fragmentImage.setImageResource(imageINT);
    instance.setArguments(bundle);
    return instance;
}

public static String getTAG() {
    return TAG;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    gson = new Gson();
    homeAdapter = new HomeAdapter(context, activity, categoy);
    homeAdapter.setCategories(topCharts);
    homeAdapter.updateItems();
    super.onCreate(savedInstanceState);

}

我该如何解决这个问题?

我知道 nullPointerExaption 的此错误,但我该如何解决?

1 个答案:

答案 0 :(得分:0)

这发生在我身上。要解决此问题,请尝试以下步骤

  1. 在项目build.gradle

    中添加以下依赖项
    dependencies {
          classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
    
  2. 将以下依赖项添加到应用build.gradle文件

    apply plugin: 'com.neenbedankt.android-apt'

    dependencies { apt 'com.jakewharton:butterknife-compiler:8.4.0' }