Lechat Noir Lechat Noir - 2 months ago 37
Java Question

Fragment activity crash on item click

I used "empty activity" but I am changing with "Navigation drawer activity".
Now, when I click on my item "ACTIVA 128", my application stop running and I don't know how to fix it.

Thank you.

CODE :

package ssip.ssip;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class test extends Fragment{

View rootview;

private Button btn_search;

ArrayAdapter adapterSpinner_1,adapterSpinner_2;
Spinner spin1,spin2;
String spn_1_Selected_item;
String[] spinn_marque = {"Veuillez choisir une centrale","ACTIVA 128"};
String[] spinn_code = {"Veuillez choisir un niveau","NIVEAU 3","NIVEAU 4"};
TextView txtResult;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.activity_menu_code, container, false);

txtResult = (TextView) rootview.findViewById(R.id.txtResult);
spin1 = (Spinner) rootview.findViewById(R.id.spin1);
spin2 = (Spinner) rootview.findViewById(R.id.spin2);
btn_search = (Button) rootview.findViewById(R.id.btn_search);

adapterSpinner_1=new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,spinn_marque);
spin1.setAdapter(adapterSpinner_1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spn_1_Selected_item = parent.getItemAtPosition(position).toString();

if (spn_1_Selected_item.equals("Veuillez choisir une centrale"))
txtResult.setText("");

if (spn_1_Selected_item.equals("ACTIVA 128")) {
String code1[] = {spinn_code[0], spinn_code[2], spinn_code[3]};
adapterSpinner_2 = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, code1);
spin2.setAdapter(adapterSpinner_2);
spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String codeSelected = parent.getItemAtPosition(position).toString();
if (codeSelected.equals("Veuillez choisir un niveau"))
txtResult.setText("");
if (codeSelected.equals("NIVEAU 3"))
btn_search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtResult.setText("7677");
}
});
if (codeSelected.equals("NIVEAU 4"))
btn_search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtResult.setText("7780");
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootview;
}
}


LOGCAT

09-25 12:54:12.135 25496-25496/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 0
09-25 12:54:12.205 25496-25496/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 1
09-25 12:54:12.225 25496-25496/ssip.ssip D/AbsListView: Get MotionRecognitionManager
09-25 12:54:12.225 25496-25496/ssip.ssip E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@1131fef
09-25 12:54:12.225 25496-25496/ssip.ssip E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ab99afc
09-25 12:54:12.225 25496-25496/ssip.ssip E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ab99afc

--------- beginning of system
09-25 12:54:12.265 25496-25496/ssip.ssip D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{d17bb2c V.E...... R.....I. 0,0-0,0}
09-25 12:54:12.295 25496-25522/ssip.ssip D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1592x832]-format:1
09-25 12:54:12.325 25496-25496/ssip.ssip W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
09-25 12:54:12.355 25496-25496/ssip.ssip D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
09-25 12:54:25.195 25496-25496/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 0
09-25 12:54:25.275 25496-25496/ssip.ssip D/ViewRootImpl: ViewPostImeInputStage processPointer 1
09-25 12:54:25.355 25496-25496/ssip.ssip D/ViewRootImpl: #3 mView = null
09-25 12:54:25.385 25496-25496/ssip.ssip E/ViewRootImpl: sendUserActionEvent() mView == null
09-25 12:54:25.385 25496-25496/ssip.ssip D/AndroidRuntime: Shutting down VM
09-25 12:54:25.385 25496-25496/ssip.ssip E/AndroidRuntime: FATAL EXCEPTION: main
Process: ssip.ssip, PID: 25496
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at ssip.ssip.test$1.onItemSelected(test.java:51)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:1165)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1154)
at android.widget.AdapterView.access$300(AdapterView.java:59)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1121)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
09-25 12:59:25.465 25496-25496/ssip.ssip I/Process: Sending signal. PID: 25496 SIG: 9

Answer

Change this code

String code1[] = {spinn_code[0], spinn_code[2], spinn_code[3]};

with

String code1[] = {spinn_code[0], spinn_code[1], spinn_code[2]};

You got this error because there is no third item of spinn_code array.

Comments