Anirudh Sohil
Anirudh Sohil

Reputation: 103

FATAL EXCEPTION:main error

Here is the log message

03-07 22:54:51.555: E/AndroidRuntime(18687): FATAL EXCEPTION: main
03-07 22:54:51.555: E/AndroidRuntime(18687): Process: com.example.simplegamer003.registerapp, PID: 18687
03-07 22:54:51.555: E/AndroidRuntime(18687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplegamer003.registerapp/com.example.simplegamer003.registerapp.SpinnerActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.os.Looper.loop(Looper.java:148)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.main(ActivityThread.java:5466)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at java.lang.reflect.Method.invoke(Native Method)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 22:54:51.555: E/AndroidRuntime(18687): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner
03-07 22:54:51.555: E/AndroidRuntime(18687):    at com.example.simplegamer003.registerapp.SpinnerActivity.onCreate(SpinnerActivity.java:22)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.Activity.performCreate(Activity.java:6251)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
03-07 22:54:51.555: E/AndroidRuntime(18687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
03-07 22:54:51.555: E/AndroidRuntime(18687):    ... 9 more

and here is my code

public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

private Spinner spinner;
private static final String[] sports = {
        "Hockey","Cricket","Football","Basketball","Badminton","Tennis"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);

    spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> adapter;
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,sports);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();
}

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

I don't know why am I getting this error please anyone tell me.

Upvotes: 0

Views: 68

Answers (3)

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

Well, obviously

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Spinner

Means that you have a LinearLayout instead of Spinner with id "@+id/spinner in activity_spinner

Upvotes: 1

TanLingxiao
TanLingxiao

Reputation: 412

In your SpinnerActivity,LinearLayout can not be cast Spinner.Which means you wirte LinearLayout in activity_spinner.xml but use spinner = (Spinner)findViewById(R.id.spinner); instead .

Upvotes: 0

Brijesh Chopda
Brijesh Chopda

Reputation: 195

If you are sure that it is spinner and you have recently made change to your layout file then just clean your project and run again will solve your problem. Sometimes modifying the layout file will not instantly reflect to R file and gives ClassCastException.

Upvotes: 0

Related Questions