Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am new to Android and don't know much. Please help me with this problem

I have a spinner which goes to another class when an option is selected and then clicked on a button. I have given string arrays in strings.xmlfile.

<string-array name="month_arrays">
        <item>2015 June</item>
        <item>2015 July</item>
        <item>2015 August</item>
        <item>2015 September</item>
        <item>2015 October</item>
        <item>2015 November</item>
        <item>2015 December</item>
        <item>2016 January</item>
        <item>2016 February</item>
        <item>2016 March</item>
    </string-array>

I have used this in a fragment class and using getStringArrays() in ArrayList and then stored it in an ArrayAdapter.

public class CalendarFragment extends Fragment {

public CalendarFragment(){}
TextView t1;
TextView t2;
Spinner spinner1;
Button btnSubmit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
     t1=(TextView)rootView.findViewById(R.id.textView1);
     t2=(TextView)rootView.findViewById(R.id.textView2);
     Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "font.otf");
     t1.setTypeface(font); 
     t2.setTypeface(font);


     String[] regionsArray = getResources().getStringArray(R.array.month_arrays);
     ArrayList<String> list = new ArrayList<String>(Arrays.asList(regionsArray));
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

        spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());


        btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(),
                        "OnClickListener : " + 
                        "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()), // This is Testing if it gets Selected
                        Toast.LENGTH_SHORT).show();
            }

        });

    return rootView;

}

}

But I get a NullPointer Exception when I go to this Menu and the app gets Force Close.

LogCat

08-09 09:14:30.857: E/AndroidRuntime(1728): FATAL EXCEPTION: main 08-09 09:14:30.857: E/AndroidRuntime(1728): Process: com.naysotech.smartrajagiri, PID: 1728 08-09 09:14:30.857: E/AndroidRuntime(1728): java.lang.NullPointerException 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.naysotech.smartrajagiri.CalendarFragment.onCreateView(CalendarFragment.java:58) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.Fragment.performCreateView(Fragment.java:1700) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.BackStackRecord.run(BackStackRecord.java:684) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Handler.handleCallback(Handler.java:733) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Handler.dispatchMessage(Handler.java:95) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Looper.loop(Looper.java:136) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-09 09:14:30.857: E/AndroidRuntime(1728): at java.lang.reflect.Method.invokeNative(Native Method) 08-09 09:14:30.857: E/AndroidRuntime(1728): at java.lang.reflect.Method.invoke(Method.java:515) 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-09 09:14:30.857: E/AndroidRuntime(1728): at dalvik.system.NativeStart.main(Native Method)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You should initiate spinner:

spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);

before you try to set adapter for it:

spinner1.setAdapter(dataAdapter);
share|improve this answer
    
Thankyou So Much!! It worked –  Alok Naushad Aug 14 at 5:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.