gpt4 book ai didi

带有 android fragment 的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 09:53:31 30 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现 fragment 。当我更改选项卡(我正在使用抽屉布局)时出现问题,我收到 java.lang.NullPointerException 错误。我测试了主要 Activity 的代码并且运行顺利。唯一的问题是当我将它应用于 fragment 时。这是代码:

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;

}

这是日志

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 中的膨胀 View 初始化您的 View

关于带有 android fragment 的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340398/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com