我正在尝试在我的应用程序中实现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 VM10-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: mainjava.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.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中的膨胀视图初始化您的视图
总结以上是内存溢出为你收集整理的带有android片段的java.lang.NullPointerException全部内容,希望文章能够帮你解决带有android片段的java.lang.NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)