简体   繁体   English

如何修复java.lang.NullPointerException(android)?

[英]How do I fix my java.lang.NullPointerException (android)?

Whenever I run my code I get an error that looks like it originates from line 14 of my code because of a java.lang.NullPointerException. 每当我运行我的代码时,由于java.lang.NullPointerException,我都会收到一个看起来像是源于我的代码第14行的错误。 Here's my code: 这是我的代码:

package com.example.android.rng;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {
    // get EditText by id
    final EditText et=(EditText)findViewById(R.id.minimum);
    final EditText tv=(EditText)findViewById(R.id.maximum);
    // set Et as an integer
    int min = Integer.parseInt(et.getText().toString());
    int max = Integer.parseInt(tv.getText().toString());
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /*
    * Generates a random number when the "Generate" button is pressed
    */
    public void randomNumGen (View View) {
        Random r = new Random();
        int i1 = r.nextInt(max - min + 1) + min;
        displayResults(i1);
    }
     /*
      * Displays RandomNumGen's product to the screen
      */
    public void displayResults (int results) {

        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.results);
        textView.setText(results);
    }
}

I have scanned through my code multiple times but cannot seem to find the problem, and I'm pretty new to coding so I don't have much experience with most of the code I am using. 我已经遍历我的代码多次,但似乎找不到问题,而且我对编码还很陌生,因此我对使用的大多数代码没有太多经验。

您的EditTexts为null,因为在您调用findViewById时,您的布局没有膨胀,因此它返回null,请在setContentView()之后在onCreate方法中调用它

You can't access objects in your layout until after the setContentView call. 您必须在setContentView调用之后才能访问布局中的对象。

Change the onCreate to this: onCreate更改为此:

// get EditText by id
final EditText et;
final EditText tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et=(EditText)findViewById(R.id.minimum);
    tv=(EditText)findViewById(R.id.maximum);

    // set Et as an integer
    int min = Integer.parseInt(et.getText().toString());
    int max = Integer.parseInt(tv.getText().toString());

Also, the way the code is now assumes that the two EditText fields will always contain numbers. 同样,现在的代码方式假定两个EditText字段将始终包含数字。 If they start out empty and the user inputs numbers you should probably move the code where you get the data inside a button press or something similar. 如果它们开始是空的并且用户输入了数字,则您可能应该将代码移动到通过按钮或类似方式获取数据的位置。

final EditText et=(EditText)findViewById(R.id.minimum);
final EditText tv=(EditText)findViewById(R.id.maximum);
// set Et as an integer
int min = Integer.parseInt(et.getText().toString());
int max = Integer.parseInt(tv.getText().toString());

place this in onCreate function ! 把它放在onCreate函数中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM