简体   繁体   中英

Variable is showing value as null even when I had set already value of it

I have created background service to connect my socket server, it works background while app is off, when user open the app MainActivity join to my service class and it looks fine, my service can change fragment in main activity, but when it get disconnected and want to change fragment in main activity then app crash

check my MainActivity

public class MainActivity extends AppCompatActivity {

    private clientService mclientService;
    private Intent mServiceIntent;
    private final FragmentManager fm = getFragmentManager(); 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mclientService = new clientService(this);
        mServiceIntent = new Intent(this, mclientService.getClass());
        if (!isMyServiceRunning(mclientService.getClass())) {
            startService(mServiceIntent);
        } 
    }
    public Fragment changeFragment (Fragment cls) {
        FragmentTransaction ft = this.fm.beginTransaction();
        ft.replace(R.id.bodyFrame, cls);
        ft.commit(); 
        return cls;
    }
 

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("isMyServiceRunning?", true+"");
                return true;
            }
        }
        Log.i ("isMyServiceRunning?", false+"");
        return false;
    }

}

and the clientService:

public class clientService extends Service {

    private Socket mSocket;
    {
        try {
            IO.Options opts = new IO.Options();
            opts.query = "_d=jakistakiid";
            mSocket = IO.socket("http://10.0.2.2:3000", opts);
        } catch (URISyntaxException e) {}
    }


    public clientService(MainActivity main) {
        super();
        mainAttach(main);
    }
    public clientService() {
    }
    public MainActivity mMain;


    public void startIt(){
        Log.i("eroapp", "Service Started");
        if(mSocket != null) {
            mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i("eroapp", "connected");
                }
            }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    Log.i("eroapp", "dc:"+mMain);
                   onDC();
                }
            });
            mSocket.connect();
        }

    } 

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        startIt();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent broadcastIntent = new Intent(this, restartReceiver.class);
        sendBroadcast(broadcastIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



    // CLIENT FUNCTIONS //
    private void onDC(){
        Log.i("eroapp", "DC:"+mMain);
    }

    private void mainAttach(MainActivity m) {
        mMain = m;
        Log.i("eroapp","Main created!");
        if(!mSocket.connected()) {
            mMain.changeFragment(new offlineFragment());
        }else{
            mMain.changeFragment(new singFragment());
        }
    }
  
}

looks fine, when i close app activity my service restarts and run in background, it says in logcat:

2019-11-17 02:41:22.686 17458-17458/com.example.secmsg I/eroapp: Service Started

2019-11-17 02:41:22.803 17458-17483/com.example.secmsg I/eroapp: connected

when i open app again, my service is already running, so its run only function mainAttach in clientService, and then service run changeFragment function in main activity, works great, but when I get disconnected from server it says mMain is null; < and logcat output:

2019-11-17 02:41:31.654 17458-17499/com.example.secmsg I/eroapp: dc:null

2019-11-17 02:41:31.654 17458-17499/com.example.secmsg I/eroapp: DCnull

The mclientService you created on the MainActivity is not the same as the clientService that was started by the Android system when you called startService(intent) . All services created and started by the Android system by using the service class' empty constructor, therefore, clientService#mainAttach will never be called. Take a look at this answer for more information.

If you want to directly interact with the service on the MainActivity , you might want to bind the service to your activity. Check out the documentation about bound services here .

Another thing... It looks like you're planning to directly control the MainActivity from the service using the reference to the activity. Please never do that and use broadcasts instead. Good luck!

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