gpt4 book ai didi

java - Android - SeekBar NullPointEreException - 我不知道为什么。

转载 作者:行者123 更新时间:2023-12-01 18:15:46 31 4
gpt4 key购买 nike

为什么我在第 86 行收到 SeekBar NullPointerException?我不明白。我在哪里俯瞰?我认为下面的两个类轮代码可以解决这个问题:

    // Get SeekBar references
mRoiYearSeekBar = (SeekBar) findViewById(R.id.roiYearSeekBar);
mRoiMonthSeekBar = (SeekBar) findViewById(R.id.roiMonthSeekBar);

如有任何反馈,我们将不胜感激。

这是问题所在的更大代码块:

public class RoiCalc extends Activity {

private EditText mRoiInitialInvestmentEditText;
private EditText mRoiReturnAmountEditText;
private SeekBar mRoiYearSeekBar;
private SeekBar mRoiMonthSeekBar;
private TextView mRoiYearTextView;
private TextView mRoiMonthTextView;
private Button mRoiResetButton;
private Button mRoiCalculateButton;

public BigDecimal benefit;
public BigDecimal cost;
private BigDecimal year;
private BigDecimal month;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


// Get transitions
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);


setContentView(R.layout.activity_roi_calc);


RoiInputCalcFrag inputFrag = new RoiInputCalcFrag();

getFragmentManager().beginTransaction().add(R.id.roiMaster, inputFrag).commit();


// Enable "Up" button
getActionBar().setDisplayHomeAsUpEnabled(true);

// Get EditText references
mRoiInitialInvestmentEditText = (EditText) findViewById(R.id.roiInitialInvestmentEditText);
mRoiReturnAmountEditText = (EditText) findViewById(R.id.roiReturnAmountEditText);


// Get SeekBar references
mRoiYearSeekBar = (SeekBar) findViewById(R.id.roiYearSeekBar);
mRoiMonthSeekBar = (SeekBar) findViewById(R.id.roiMonthSeekBar);


mRoiYearTextView = (TextView) findViewById(R.id.roiYearTextView);
mRoiMonthTextView = (TextView) findViewById(R.id.roiMonthTextView);

// Get Button references
mRoiResetButton = (Button) findViewById(R.id.roiResetButton);
mRoiCalculateButton = (Button) findViewById(R.id.roiCalculateButton);

// Set SeekBar progress to the default value on the layout
mRoiYearTextView.setText(String.valueOf(mRoiYearSeekBar.getProgress()));
mRoiMonthTextView.setText(String.valueOf(mRoiMonthSeekBar.getProgress()));

日志记录

04-16 22:27:52.499: E/WifiStateMachine(547): WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=1.20 rxSuccessRate=0.67 targetRoamBSSID=any RSSI=-72
04-16 22:27:53.429: I/ActivityManager(547): START u0 {cmp=uk.co.example.calculator/.activities.RoiCalc} from uid 10156 on display 0
04-16 22:27:53.466: D/AndroidRuntime(9117): Shutting down VM
04-16 22:27:53.467: E/AndroidRuntime(9117): FATAL EXCEPTION: main
04-16 22:27:53.467: E/AndroidRuntime(9117): Process: uk.co.example.calculator, PID: 9117
04-16 22:27:53.467: E/AndroidRuntime(9117): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.example.calculator/uk.co.example.calculator.activities.RoiCalc}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SeekBar.getProgress()' on a null object reference
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.os.Looper.loop(Looper.java:135)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-16 22:27:53.467: E/AndroidRuntime(9117): at java.lang.reflect.Method.invoke(Native Method)
04-16 22:27:53.467: E/AndroidRuntime(9117): at java.lang.reflect.Method.invoke(Method.java:372)
04-16 22:27:53.467: E/AndroidRuntime(9117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-16 22:27:53.467: E/AndroidRuntime(9117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-16 22:27:53.467: E/AndroidRuntime(9117): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SeekBar.getProgress()' on a null object reference
04-16 22:27:53.467: E/AndroidRuntime(9117): at uk.co.example.calculator.activities.RoiCalc.onCreate(RoiCalc.java:86)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.Activity.performCreate(Activity.java:5933)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-16 22:27:53.467: E/AndroidRuntime(9117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-16 22:27:53.467: E/AndroidRuntime(9117): ... 10 more
04-16 22:27:53.469: W/ActivityManager(547): Force finishing activity uk.co.example.calculator/.activities.RoiCalc
04-16 22:27:53.477: W/ActivityManager(547): Force finishing activity uk.co.example.calculator/.MainActivity

最佳答案

mRoiYearSeekBar = (SeekBar) findViewById(R.id.roiYearSeekBar);

如果 id roiYearSeekBar 不存在,mRoiYearSeekBar 将为 null。

您的搜索栏 ID 之一不存在,因此 SeekBar 为空。

关于java - Android - SeekBar NullPointEreException - 我不知道为什么。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688660/

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