【问题标题】:Fragment causing NPE upon starting service with onCheckedChanged使用 onCheckedChanged 启动服务时导致 NPE 的片段
【发布时间】:2014-03-06 17:46:05
【问题描述】:

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

然而,发生的事情是,每当我切换开关时,我都会得到一个空指针异常。错误日志是here

我能做些什么来解决这个问题?我是否应该将按钮的代码扔到主要活动而不是片段上?这不是问题,但我真的很想在片段中包含它。

我的片段:

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;
}

}

【问题讨论】:

  • 你在哪里初始化SharedPreferences.Editor引用?
  • 哇,你说得对。我的错。添加 service_status = this.getActivity().getSharedPreferences("ServiceStatus", 0); edit_status = this.service_status.edit();到 onCreateView

标签: android service nullpointerexception fragment


【解决方案1】:

没有初始化你的:edit_status

在使用它之前:

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

【讨论】:

  • 我的错。那效果很好。但是,我在 logcat 中遇到错误。 W/ActivityManager(960):无法启动服务 Intent { cmp=com.example.test。 aospview.a_view/com.example.test.aospview.services.SensorService } U=0:未找到 EDIT 清单中的服务放置在 修复之前。
  • 检查 ctx,getActivity,确保它不为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
相关资源
最近更新 更多