Pergunta

I've a problem to implement a java interface on my Android app... The app crashes when the event is called. Specifically, on line "listener.callBack(String.valueOf(stringListValue.get(position)));" This is the Logcat:

03-29 15:23:38.659  12568-12568/com.robertot.timereport E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.robertot.timereport, PID: 12568
    java.lang.NullPointerException
            at com.robertot.timereport.com.robertot.timereport.pages.MainActivity$2.onItemSelected(MainActivity.java:231)
            at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
            at android.widget.AdapterView.access$200(AdapterView.java:48)
            at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

This is the MainActivity:

    public class MainActivity extends FragmentActivity
        {
            //[...]

            private OnSelectItemSpinner listener;

            public interface OnSelectItemSpinner
            {
                public void callBack(String idjob);
            }

            @Override
            protected void onCreate(Bundle savedInstanceState) { //[...] 
}

        //[...]
        ArrayAdapter<String> spnAdapt = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, stringListName);
                    spnAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spnJobList.setAdapter(spnAdapt);
                    spnJobList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
                    {
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
                        {
                            switch (FragmentTag)
                            {
                                //Summary - Refresh ListView
                                case 0:
//CRASH HERE...
                                    listener.callBack(String.valueOf(stringListValue.get(position)));
                                    break;
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0){}
                    });
    //[...]

This is the Fragment when I call the interface:

public class SummaryFragment extends Fragment implements MainActivity.OnSelectItemSpinner 
{

@Override
    public void callBack(String idjob)
    {
        //do work
    }
}

When I wrong??

Thank you!!!

Foi útil?

Solução

If you want to communicate between activity and fragments you must notice to this tip: To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

Please refer to Communicating Between Activity and Fragment

If you want to communicate in reverse you can do this code:

public class MainFragment extends Fragment {

    public void callBack() {
        // Do SomeThing
    }
}


public class MainActivity extends Activity { 

    public void callFragmentCallBack() {
        MainFragment fragment = (MainFragment) getFragmentManager().findFragmentById(R.id.fragment_main);
        fragment.callBack(); // This line is important
    }
}

Outras dicas

You are getting NullPointerException because You don't initialize listener anywhere.

OnSelectItemSpinner listener = new OnSelectItemSpinner()
{
    @Override
    public void callBack(String idjob)
    {
        // Do smth
    }
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top