简体   繁体   中英

how to add condition to spinner on android

i have a spinner in android, i want to check if value in spinner return "LAIN-LAIN" on index 6 it willl visible my edittext, but when i run it, it return to error, this is my code :

ArrayAdapter<String> spinner_bukti = new ArrayAdapter<String>(Pemegang_polis.this, R.layout.spinner_style,identitas);
    spinner_bukti.setDropDownViewResource(R.layout.spinner_dropdown);
    bukti_identitas_pp.setAdapter(spinner_bukti);  

    bukti_identitas_pp.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            String Text = bukti_identitas_pp.getSelectedItem().toString();
            if (Text=="LAIN-LAIN"){
                edit_bukti_lain_pp.setVisibility(View.VISIBLE);
                teks_bukti_lain_pp.setVisibility(View.GONE);
            }else{
                edit_bukti_lain_pp.setVisibility(View.GONE);
                teks_bukti_lain_pp.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });

this is my logcat :

11-08 15:04:13.329: E/AndroidRuntime(2845): FATAL EXCEPTION: main
11-08 15:04:13.329: E/AndroidRuntime(2845): java.lang.NullPointerException
11-08 15:04:13.329: E/AndroidRuntime(2845):     at id.co.ajsmsig.formspaj.Pemegang_polis$2.onItemSelected(Pemegang_polis.java:170)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.widget.AdapterView.access$200(AdapterView.java:49)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.os.Handler.handleCallback(Handler.java:615)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.os.Looper.loop(Looper.java:137)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at android.app.ActivityThread.main(ActivityThread.java:4895)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at java.lang.reflect.Method.invoke(Method.java:511)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
11-08 15:04:13.329: E/AndroidRuntime(2845):     at dalvik.system.NativeStart.main(Native Method)

i don't know where is my fault.. i hope someone can help me to solve my problem.

First of all use:

String text = bukti_identitas_pp.getSelectedItem().toString();
if (text.equals("LAIN-LAIN")){
    ...
}

The NullPointerException seems to arise because the edit_bukti_lain_pp or the teks_bukti_lain_pp is not initialized correctly. (At least that would be my first guess...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM