带有android片段的java.lang.NullPointerException

我正在尝试在我的应用程序中实现Fragments.当我更改标签(我使用抽屉布局)时出现问题,我收到java.lang.NullPointerException错误.我测试了主要活动的代码并且运行顺利.唯一的问题是我在片段上应用它.这是代码:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class FragW extends Fragment {
    Spinner spinwatt ;
    Button calculate;
    EditText wattEdit;
    EditText costEdit;
    TextView resultText;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragw,container,false);
        calculate = (Button) getView().findViewById(R.id.calculate);
        wattEdit = (EditText) getView().findViewById(R.id.wattEdit);
        costEdit = (EditText) getView().findViewById(R.id.costEdit);

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float wattVar = Float.parseFloat(wattEdit.getText().toString());
                float costvar = Float.parseFloat(costEdit.getText().toString());

                if (spinwatt.getSelectedItem().toString().equals("µW"))
                    wattVar = wattVar * (float) 0.000001;
                else if (spinwatt.getSelectedItem().toString().equals("mW"))
                    wattVar = wattVar * (float) 0.001;
                else if (spinwatt.getSelectedItem().toString().equals("kW"))
                    wattVar = wattVar * 1000;
                String resultvar = Float.toString(costvar * wattVar);
                resultText.setText(resultvar);

            }



        });
        spinwatt = (Spinner)getView().findViewById(R.id.spinwatt);
        ArrayAdapter<CharSequence> wattAdapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.wattArray, android.R.layout.simple_spinner_item);
        wattAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinwatt.setAdapter(wattAdapter);

        return v;

    }

这是logCat

10-13 12:27:13.429    2563-2563/az.test2 D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
10-13 12:27:31.468    2563-2563/az.test2 D/AndroidRuntime﹕ Shutting down VM
10-13 12:27:31.468    2563-2563/az.test2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
10-13 12:27:31.488    2563-2563/az.test2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at az.test2.FragW.onCreateView(FragW.java:25)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
        at android.app.BackStackRecord.run(BackStackRecord.java:622)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4340)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

第25行是:calculate =(Button)getView().findViewById(R.id.calculate);.但是,所有这样的行都会导致此错误(第25,26,27,55行).我还尝试用定义初始化变量(在第16行到第20行)我仍然得到了相同的错误.
请告诉我应该怎么做.

解决方法:

改变这一点

    calculate = (Button) getView().findViewById(R.id.calculate);
    wattEdit = (EditText) getView().findViewById(R.id.wattEdit);
    costEdit = (EditText) getView().findViewById(R.id.costEdit);

    calculate = (Button) v.findViewById(R.id.calculate);
    wattEdit = (EditText) v.findViewById(R.id.wattEdit);
    costEdit = (EditText) v.findViewById(R.id.costEdit);

从Fragment中的膨胀视图初始化您的视图

上一篇:Java – Array中的NullPointerException


下一篇:java – JLabel不会显示图像 – NullPointerException