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:
- cómo establecer la flecha desplegable en el hilandero con el fondo?
- Imagen de estiramiento en la imagen para ajustar la pantalla en xml
- ¿Cómo podemos utilizar android: inputType en EditTextPreference?
- La vista personalizada no aparece
- Error: Los tipos de cadena no están permitidos (en 'layout_height' con el valor 'wrap content'). activity_main.xml
@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.
- ¿Puedo dar un ID a un elemento en String-array?
- Alturas de diseño de tramo de Android para que el diseño ocupa el 50% de la pantalla
- Comentarios en Android Layout xml
- Cifrar y descifrar xml
- Android TextView DrawableTint en dispositivos pre v23
- Post datos UTF-8 codificados en el servidor pierde ciertos caracteres
- cómo hacer que el diseño haga clic de forma programable
- Android: guardar el estado del diseño cambiado dinámicamente
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