片段导致NPE在与onCheckedChanged开始服务 [英] Fragment causing NPE upon starting service with onCheckedChanged

查看:178
本文介绍了片段导致NPE在与onCheckedChanged开始服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态显示片段的活性。默认片段有一个开关,如果启用,启动的服务。

I have an activity which displays fragments dynamically. The default fragment has a switch that, if enabled, starts a service.

但是发生了什么事,就是每当我切换开关,我得到一个NullPointerException异常。错误日志是这里

What's happening however, is that whenever I toggle the switch I get a nullpointerexception. The errorlog is here

我能做些什么来解决这个问题?也许我应该扔code的按钮经过上的主要活动,而不是片段?这不会是一个问题,但我真的很想拥有它的片段。

What can I do to fix this? Should I perhaps throw the code for the button over onto the main activity instead of the fragment? This wouldn't be a problem, but I'd really like to have it in the fragment.

我的片段:

public class DefaultScreen extends Fragment {

SharedPreferences.Editor edit_status;
private SharedPreferences service_status;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /** Inflating the layout for this fragment **/
    View v = inflater.inflate(R.layout.fragment_default, null);

    Switch sEnable = (Switch) v.findViewById(R.id.switch_service);
    Switch sRoot = (Switch) v.findViewById(R.id.switch_root);

    sEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            Toast.makeText(getActivity(), "Service Switch State = " +isChecked, Toast.LENGTH_SHORT).show();
            Context ctx = (Context)DefaultScreen.this.getActivity();
            if(isChecked)
            {
            edit_status.putBoolean("ServiceMode", true).commit();
            ctx.startService(new Intent(getActivity(), SensorService.class));

            } else {
                edit_status.putBoolean("ServiceMode", false).commit();
                ctx.stopService(new Intent(getActivity(), SensorService.class));
            }
        }
    });

    sRoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            Toast.makeText(getActivity(), "Root Switch State = " +isChecked, Toast.LENGTH_SHORT).show();
        }
    });

    return v;
}

private boolean proximityService(Context context) {
    @SuppressWarnings("rawtypes")
    Iterator iterator = ((ActivityManager) context.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE).iterator();


    while (iterator.hasNext()) {
        ActivityManager.RunningServiceInfo runningServiceInfo = (ActivityManager.RunningServiceInfo) iterator
                .next();
        if (SensorService.class.getName().equals(
                runningServiceInfo.service.getClassName())) {
            return true;
        }
    }

    return false;
}

}

推荐答案

尚未初始化: edit_status

在使用它之前:

edit_status.putBoolean("ServiceMode", true).commit();

这篇关于片段导致NPE在与onCheckedChanged开始服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆