2016-09-25 114 views
0

我使用了「空閒活動」,但我正在更改「導航抽屜活動」。 現在,當我點擊我的項目「ACTIVA 128」時,我的應用程序停止運行,我不知道如何修復它。片段活動崩潰項目單擊

謝謝。

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 = [email protected] 
09-25 12:54:12.225 25496-25496/ssip.ssip E/MotionRecognitionManager: motionService = [email protected]ab99afc 
09-25 12:54:12.225 25496-25496/ssip.ssip E/MotionRecognitionManager: motionService = [email protected]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 
+2

可能的重複[什麼導致java.lang.ArrayIndexOutOfBoundsException,我該如何防止它?](http://stackoverflow.com/questions/5554734/what-c​​auses-a-java-lang-arrayindexoutofboundsexception-and-我該怎麼辦 - 防止它) –

+1

@Lechat我的答案解決了你的問題嗎?如果沒有,請給我們一個反饋。 – ziLk

+0

是的^^謝謝 –

回答

2

改變此密碼

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

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

你得到了這個錯誤,因爲沒有第三個spinn_code數組。