There is a class:

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { StartActivity startActivity = new StartActivity(); SurfaceView mSurfaceView; SurfaceHolder mSurfaceHolder; Bitmap bitmap; Imageview imageview; public MainGamePanel(Context context, AttributeSet attributeSet) { super(context, attributeSet); getHolder().addCallback(this); bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.one); StartActivity.SetTime(5); //StartActivity.imageView1.setImageBitmap(bitmap);//в этом случае , как и в случае использования сеттера получаю Null } 

In the activit class:

 public class StartActivity extends AppCompatActivity { ImageView; Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.one); imageView1 = (ImageView) this.findViewById(R.id.imageView1); } public void SetTime (int Seconds1){ imageView1.setImageBitmap(bitmap); } 

.

 <view class="example.igeniy.MainGamePanel" android:id="@+id/surface" android:layout_width="match_parent" android:layout_height="match_parent"> </view> <android.support.v7.widget.LinearLayoutCompat android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:id="@+id/imageView" android:layout_gravity="center_horizontal|top" android:layout_height="wrap_content" /> </android.support.v7.widget.LinearLayoutCompat> </FrameLayout> 

If strings

 bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.one); imageView = (ImageView) this.findViewById(R.id.imageView) imageView.setImageBitmap(bitmap); 

transfer to the OnCreate method, then everything is displayed.

  • What is the question? - rjhdby
  • I need to assign Imageview various images from the MainGamePanel class - Turalllb
  • Here I am looking at your xml and I can not understand how you inside <view class="example.igeniy.MainGamePanel" want to find android:id="@+id/imageView" ? you don't need this in .findViewById(R.id.imageView) , but your Activity is rjhdby
  • I once did not try. I declared imageview in the Activity and tried to access it through an instance of the class, the result is the same. I created the Setter method and wrote imageView.setImageBitmap (bitmap) to it. And all the same error. It is worth transferring imageView.setImageBitmap (bitmap) to the OnCreate method as it is displayed, but holding it in OnCreate I will not be able to change it from another class - Turalllb
  • Already there are suspicions that in this way it is impossible to apply at all. Only using certain Inflatter (s), but what it is and how to apply, I still do not understand - Turalllb

1 answer 1

In line:

 imageView = (ImageView) this.findViewById(R.id.imageView) 

the findViewById(...) method of the MainGamePanel class object is MainGamePanel , which is a subclass of the SurfaceView class, which, in turn, is inherited from the View class.

Since in the MainGamePanel and SurfaceView the findViewById(...) method is not overridden, the implementation of this method from the View class View called.

The findViewById(...) method, called on some View v object, searches among the child views of this View v widget with the passed identifier.

Since MainGamePanel no child views, the findViewById(...) method returns null .

Next, a call is made to the (non-static) setImageBitmap method on the null object, hence the NullPointerException .

How to fix this problem globally and correctly - to revise the architecture of what you are trying to do.

How to fix badly and locally - for example, in MainGamePanel , you can send a link to a MainGamePanel already found somewhere else (where you can find it) with the identifier R.id.imageView (for example, in the same onCreate(...) method ).

  • I already did this, trying to locally solve the problem. I declare in the class inherited from AppCompatActivity Imageview. after find it by id. Suppose this class is called StartActivity. In MainGamePanel, I write through an instance of the StartActivity class: startActivity.ImageView.setImageBitmap (bitmap) and get the same error again. - Turalllb
  • Or like this: Create a setter in StartActivity. I write ImageView.setImageBitmap (bitmap) in it and get an error again. The setter method itself is called, if Print is put in it, then it is executed. And the code is correct, if ImageView.setImageBitmap (bitmap) is moved to OnCreate, it is also executed, but as through a setter, then NPE - Turalllb
  • @Turalllb; Show the code in which all this is present and the stack-exception exception. - post_zeew
  • OK. By the way, if you check through a class instance, then ImageView is still Null. - Turalllb
  • one
    @Turalllb; When you explicitly create activites with new StartActivity() , the methods of its life cycle are not called, in particular, the onCreate(...) method is not called, hence the initialization of the views does not occur, therefore ImageView will be null . - post_zeew