-1

This is XML spinner

<android.support.v7.widget.AppCompatSpinner
                android:id="@+id/spinnerNmKategori"
                style="?android:attr/spinnerStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/white"
                android:entries="@array/nmKat"
                android:spinnerMode="dialog" />

This is my code

private void simpanPrd() {
    sKdPrd = kdprd.getText().toString();
    sNmPrd = nmprd.getText().toString();
    sNmKat = spinKategori.getSelectedItem().toString();
    if (sKdPrd.isEmpty()){
        kdprd.setError("Kode Produk tidak boleh kosong");
        kdprd.requestFocus();
    } else if (sNmPrd.isEmpty()){
        nmprd.setError("Nama Produk tidak boleh kosong");
        nmprd.requestFocus();
    } else if (sNmKat.equals("Pilih Kategori")){
        ((TextView)spinKategori.getSelectedView()).setError("Your Error msg Here");
    }

}

When I click my button, and check spinner if not selected kategori. I want to set error like an EditText in the spinner. However when I run my app I always get an error. This is my error

02-19 17:56:58.834 16939-16939/com.example.steven.bellishopadmin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.steven.bellishopadmin, PID: 16939
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
    at com.example.steven.bellishopadmin.View.TambahProduk.simpanPrd(TambahProduk.java:178)
    at com.example.steven.bellishopadmin.View.TambahProduk.access$700(TambahProduk.java:33)
    at com.example.steven.bellishopadmin.View.TambahProduk$7$2.onClick(TambahProduk.java:152)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5264)
    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:900)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

How do I set an error on the spinner?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Steven Wiaji
  • 44
  • 1
  • 8
  • Possible duplicate of [Creating a setError() for the Spinner](https://stackoverflow.com/questions/3749971/creating-a-seterror-for-the-spinner) – João Carlos Feb 19 '19 at 11:06
  • 1
    `android.widget.RelativeLayout cannot be cast to android.widget.TextView` tells you all. – Phantômaxx Feb 19 '19 at 12:02

2 Answers2

0

If your spinner is set-up with default item views the getSelectedView() method on Spinner class will return a TextView. And on that you can call setError(CharSequence) Here is what I did:

View selectedView = spinner.getSelectedView();
kif (selectedView != null && selectedView instanceof TextView) {
TextView selectedTextView = (TextView) selectedView;
if (!valid) {
    String errorString = selectedTextView.getResources().getString(mErrorStringResource);
    selectedTextView.setError(errorString);
}
else {
    selectedTextView.setError(null);
}
}
Ram Mohan dubey
  • 150
  • 1
  • 9
0

Your error is in this line:

((TextView)spinKategori.getSelectedView()).setError("Your Error msg Here");

Here you are casting the selected view to a TextView. The selected view seems to be a RelativeLayout and that's why you receive this class cast exception.

Chris623
  • 2,464
  • 1
  • 22
  • 30