Cómo cambiar el origen de ImageView en android

Este es mi xml, este se encuentra en un fragmento que aparece en mi actividad.

<FrameLayout android:id="@+id/frame1" android:layout_width="wrap_content" android:layout_height="115dp" android:layout_margin="2dp" android:layout_weight="0.33"> <ImageView android:id="@+id/whoamiwith" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:src="@drawable/default_image" /> </FrameLayout> 

Y este es mi código java:

 @Override public void onClick(View click) { if (click == profileBtn) { whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put); } } 

Estoy tratando de cambiar el origen de la imagen de la imagen, no hay ningún error de sintaxis, pero cuando ejecuto el botón el emulador me está dando una fuerza cerca, y en el logcat dice java.lang.NULLPointerException , y su está apuntando Al whoamiwith.setBackgroundResource(R.drawable.loginbtn); línea.

 whoamiwith.setImageResource(R.drawable.loginbtn); 
  ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith) Drawable new_image= getResources().getDrawable(R.drawable.loginbtn); whoamiwith.setBackgroundDrawable(new_image); 

Sólo inténtalo

 ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith) whoamiwith.setImageResource(R.id.new_image); 

Inicializar vista de imagen:

 whoamiwith = findViewByid(R.id.whoamiwith); 

A continuación, en el método Click, escriba estas líneas para cambiar el recurso de la imagen:

  if(android.os.Build.VERSION.SDK_INT > 15) { // for API above 15 whoamiwith.setBackground(getResources().getDrawable(R.drawable.loginbtn)); } else { // for API below 15 whoamiwith.setBackgroundDrawable(getResources().getDrawable(R.drawable.loginbtn)); } 
 ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith); whoamiwith.setImageResource(R.drawable.image_i_wanna_put); 

La excepción es whoamiwith es null. whoamiwith u inicializar whoamiwith como, ImageView whoamiwith = (ImageView) findViewById (R.id.whoamiwith)

Consulte Utilización de setImageDrawable dinámicamente para configurar la imagen en un ImageView

  • Diseño responsable para todos los dispositivos Android
  • ¿Es posible crear esta forma simple en Android XML?
  • Haciendo complejos Android XML Drawables
  • Cómo comprobar NULL en etiquetas XML XML analizadas
  • Cómo hacer que Android soporte FloatingActionButton en la parte inferior derecha de la pantalla?
  • Columnas para Android GridView Fit
  • Símbolo de porcentaje en strings.xml
  • Analizar xml de la respuesta httppost
  • Android / Eclipse / Final prematuro del archivo xml
  • No se puede arrastrar DrawerLayout desde ListView
  • Android: ¿cómo usar los colores del tema actual en un xml extraíble?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.