Well you will see friends, I am developing a project which involves taking 4 pictures from the camera to my device , the problem is when I'm taking pictures and when you go to capture some of them to return to my activity sends me this error.
04-08 21:44:39.493 28084-28084/com.example.user.activofijo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.activofijo, PID: 28084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.activofijo/com.example.user.activofijo.AgregarActivo}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2480)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4293)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5725)
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:1030)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
at com.example.user.activofijo.AgregarActivo.selectionspinner(AgregarActivo.java:1174)
at com.example.user.activofijo.AgregarActivo.onCreate(AgregarActivo.java:151)
at android.app.Activity.performCreate(Activity.java:6018)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2480)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4293)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5725)
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:1030)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
04-08 21:44:39.493 840-3251/? E/ActivityManager: App crashed! Process: com.example.user.activofijo
So indicates the error is on the part of the spinner.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
I have 4 spinners in the activity where each load data received from a server just put the code where stated .
public class AgregarActivo extends ActionBarActivity implements OnClickListener {
Spinner spinner_oficina;
Spinner spinner_departamento;
Spinner spinner_personal;
Spinner spinner_categoria;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agregar_activo);
selectionspinner();
}
public void selectionspinner(){
spinner_oficina = (Spinner) findViewById(R.id.spinner_oficina);
spinner_departamento = (Spinner)findViewById(R.id.spinner_departamento);
spinner_personal = (Spinner) findViewById(R.id.spinner_personal);
spinner_categoria = (Spinner) findViewById(R.id.spinner_categoria);
spinner_oficina.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int id_oficinapost = arrayoficinas.get(pos).getIdOficina();
String res1 = String.valueOf(id_oficinapost);
tvO.setText(res1);
spinnerdepartamento(res1);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner_departamento.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int id_departamento = arraydepartamentos.get(pos).getIdDepartamento();
String res2 = String.valueOf(id_departamento);
tvD.setText(res2);
spinnerpersonal(res2);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner_personal.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Integer id_personalpost = arraypersonal.get(pos).getIdpersonal();
String res3 = String.valueOf(id_personalpost);
tvP.setText(res3);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner_categoria.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int id_categoriapost = arraycategorias.get(pos).getIdcategoria();
String res4 = String.valueOf(id_categoriapost);
tvC.setText(res4);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
Code picture.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
//------------------CÓDIGO DE BARRAS------------------------
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
//Quiere decir que se obtuvo resultado pro lo tanto:
//Desplegamos en pantalla el contenido del código de barra scaneado
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
//Desplegamos en pantalla el nombre del formato del código de barra scaneado
String scanFormat = scanningResult.getFormatName();
formatTxt.setText(scanFormat);
capturas_scan = capturas_scan + 1;
}
if (scanningResult == null) {
//Quiere decir que NO se obtuvo resultado
if (capturas_scan == 1) {
capturas_scan = capturas_scan - 1;
Toast toast = Toast.makeText(getApplicationContext(),
"No se ha recibido datos del scaneo!", Toast.LENGTH_SHORT);
toast.show();
} else {
if(contentTxt.getText().equals("")) {
Toast toast = Toast.makeText(getApplicationContext(),
"No se ha recibido datos del scaneo!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
//FOTOGRAFIA
if (capturas_scan == 0) {
LayoutInflater inflater = LayoutInflater.from(this);
int id = R.layout.layout_right;
LinearLayout relativeLayout = (LinearLayout) inflater.inflate(id, null, false);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT;
params.topMargin = 100;
relativeLayout.setPadding(5, 3, 5, 3);
//relativeLayout.setPadding(5, 3, 5, 3);
relativeLayout.setLayoutParams(params);
/**
* Se revisa si la imagen viene de la c‡mara (TAKE_PICTURE) o de la galer’a (SELECT_PICTURE)
*/
if (requestCode == TAKE_PICTURE) {
/**
* Si se reciben datos en el intent tenemos una vista previa (thumbnail)
*/
if (data != null) {
/**
* En el caso de una vista previa, obtenemos el extra ÒdataÓ del intent y
* lo mostramos en el ImageView
*/
try {
if (data.hasExtra("data")) {
iv = (ImageView) relativeLayout.findViewById(R.id.imgFoto);
iv.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
BitmapDrawable estirable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = estirable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
byte[] bb = bos.toByteArray();
image = Base64.encodeToString(bb, Base64.NO_WRAP);
if (capturas == 0) {
v.add(0, image);
} else
v.add(capturas, image);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Hubo un error" + e, Toast.LENGTH_SHORT).show();
}
/**
* De lo contrario es una imagen completa
*/
} else {
/**
* A partir del nombre del archivo ya definido lo buscamos y creamos el bitmap
* para el ImageView
*/
iv = (ImageView) relativeLayout.findViewById(R.id.imgFoto);
iv.setImageBitmap(BitmapFactory.decodeFile(name));
/**
* Para guardar la imagen en la galer’a, utilizamos una conexi—n a un MediaScanner
*/
new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null;
{
msc = new MediaScannerConnection(getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile(name, null);
}
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
};
}
/**
* Recibimos el URI de la imagen y construimos un Bitmap a partir de un stream de Bytes
*/
capturas = capturas + 1;
} else if (requestCode == SELECT_PICTURE) {
Uri selectedImage = data.getData();
InputStream is;
try {
is = getContentResolver().openInputStream(selectedImage);
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
iv = (ImageView) findViewById(R.id.imgFoto);
iv.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
}
}
layout.addView(relativeLayout);
//scroll to last element
//http://stackoverflow.com/questions/6438061/can-i-scroll-a-scrollview-programmatically-in-android
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}catch (Exception e){
Toast.makeText(getApplicationContext(),"Ha habido un error"+e,Toast.LENGTH_LONG).show();
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.activofijo.AgregarActivo">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/textViewInfoUsuario" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewResId"
android:visibility="gone" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView3"
android:layout_marginTop="20dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scan_button"
android:background="@null"
android:src="@drawable/barras"
android:layout_marginTop="10dp" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scan_content"
android:layout_gravity="bottom"
android:hint="@string/numero_serie"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/info_cam"
android:id="@+id/textView16"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Obtener"
android:background="@null"
android:id="@+id/btnPic"
android:src="@drawable/camara"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:onClick="addRight" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left">
<HorizontalScrollView
android:id="@+id/scrollView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonsLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:scrollbarStyle="outsideInset" >
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:text="@string/oficina"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewImagen" />
<Spinner
android:id="@+id/spinner_oficina"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/texto_oficina"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/texto_oficina"
android:layout_below="@+id/texto_oficina"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvOficina"
android:visibility="visible" />
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="@string/departamento"
android:textSize="20dp"/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_departamento"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvDepartamento"
android:visibility="visible" />
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="@string/encargado_activo"
android:id="@+id/textView5"
android:textSize="20dp"
android:layout_marginBottom="10dp" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_personal"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvPersonal"
android:visibility="visible" />
<TextView
android:id="@+id/scan_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scan_button"
android:visibility="gone" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/categoria"
android:id="@+id/textView15"
android:textSize="20dp"
/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_categoria"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvCategoria"
android:visibility="visible" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>