片段中的空变量和空视图

时间:2015-04-10 01:26:58

标签: android android-fragments null switch-statement

我正在使用NavigationDrawer和Fragments。我上面写的片段中有一个开关。如果交换机被激活或者不使用它作为方法的参数,我需要得到它但是当我在声明它之后调用交换机时,变量给我null以及View

编辑:问题是该片段在OnCreateView中创建了视图并在屏幕上正确显示了所有内容,但当我尝试在方法中访问rootview以修改switchP时给我null,我不知道为什么。

Switch switchP;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.valor_unico, container, false);

    switchP = (Switch) rootview.findViewById(R.id.switchP);

    return rootview;
}


public String processProp(String prop) {
    switch ( calc.controlarSintaxis(prop)){
        case 0:
            return calc.resultadoValUnico(prop, switchP.isActivated());
        case 1:
            return "Error";
        case -1:
            return "Other error";
        default: return null;
    }
}

以下是错误的日志:

`04-10 14:26:24.142  15494-15494/com.hernan.calculadorlogicov31 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hernan.calculadorlogicov31, PID: 15494
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3827)
        at android.view.View.performClick(View.java:4442)
        at android.view.View$PerformClick.run(View.java:18473)
        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:5105)
        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:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3822)
        at android.view.View.performClick(View.java:4442)
        at android.view.View$PerformClick.run(View.java:18473)
        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:5105)
        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:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
        at com.hernan.calculadorlogicov31.vista.ValorUnico.processProp(ValorUnico.java:40)
        at com.hernan.calculadorlogicov31.vista.Main.mostrarResultado(Main.java:101)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3822)
        at android.view.View.performClick(View.java:4442)
        at android.view.View$PerformClick.run(View.java:18473)
        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:5105)
        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:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)`

0 个答案:

没有答案