-1
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.temp);

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.example.administrator.correct.MainActivity$1.onItemClick(MainActivity.java:65)

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 1
    Your `ImageView` is `null`, because there is no image named `R.id.imageView` in your activity at the time you call `findViewById()`. If you want somebody to help you further, please provide a [mcve]. – CommonsWare Aug 28 '16 at 14:07
  • Provide the code so we can help you, please. – Augusto Carmo Aug 28 '16 at 14:07
  • Or refer to http://stackoverflow.com/questions/3264610/findviewbyid-returns-null – Tunaki Aug 28 '16 at 14:11
  • RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout); final ImageView imageView = (ImageView)relativeLayout.findViewById(R.id.imageView); – user6767113 Aug 29 '16 at 04:29
  • imageView.setImageResource(R.drawable.temp); – user6767113 Aug 29 '16 at 04:30
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference at com.example.administrator.correct.MainActivity.onCreate(MainActivity.java:59) – user6767113 Aug 29 '16 at 04:30

1 Answers1

0

Make sure, inflated layout has and ImageView that has imageView id.

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30