方法给出错误,使方法返回void

时间:2017-10-23 11:56:34

标签: java android

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
}

显示错误:

  

make方法CreateStep return语句 void

为什么?

3 个答案:

答案 0 :(得分:0)

如果发生异常,您必须返回一些内容:

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
        return null;
    }
}

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
    return null;
}

答案 1 :(得分:0)

您必须始终返回Step对象。如果代码中存在异常,则永远不会返回Step对象。

尝试这样的事情:

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
    return new Step(); // or return null;
}

答案 2 :(得分:0)

这样做。

public Step createStep(int position) {
try {
    final StepFragmentSample step = new StepFragmentSample();
    Bundle b = new Bundle();
    b.putInt(CURRENT_STEP_POSITION_KEY, position);
    step.setArguments(b);

} catch (Exception e) {
    e.printStackTrace();
    Log.e("/Test", "/Excp due to" + e.toString());
}
 return step;
}