Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改活动时出现NullPointerException_Android_Button_Android Activity_Nullpointerexception - Fatal编程技术网

Android 更改活动时出现NullPointerException

Android 更改活动时出现NullPointerException,android,button,android-activity,nullpointerexception,Android,Button,Android Activity,Nullpointerexception,我对Android开发非常陌生,我有一个关于NullPointerException的快速问题。当从第一个“活动”页面按下按钮时,它将切换到第二个“活动”,其中有一个NullPointerException。 下面是main.xml“next”按钮(按下该按钮可切换活动)的代码: 按下“下一步”按钮时,它将切换到练习活动: import android.app.Activity; import android.content.Intent; import android.os.Bundle; i

我对Android开发非常陌生,我有一个关于
NullPointerException
的快速问题。当从第一个“活动”页面按下按钮时,它将切换到第二个“活动”,其中有一个
NullPointerException
。 下面是main.xml“next”按钮(按下该按钮可切换活动)的代码:

按下“下一步”按钮时,它将切换到练习活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class CalorieIntakeCalculator extends Activity {
    /** Called when the activity is first created. */

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

        setContentView(R.layout.main);

    }

    public void next(View view) {
        Intent intentExercise = new Intent(view.getContext(), Exercise.class);
        startActivityForResult(intentExercise, 0);
    } 
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Exercise extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.exercise);

        final EditText weightField = (EditText) findViewById(R.id.EditTextWeight);
        try {
            ((Global) this.getApplication()).setWeight(Integer.parseInt(weightField.getText().toString()));
        } catch(NumberFormatException nfe) {
            System.out.println("Could not parse " + nfe);
        }   
在exercise类中,我将ContentView设置为exercise.xml。 在接下来的几条语句中,我想将全局整数权重设置为用户在EditTextWeight EditText框中输入的数字。 下面是Global.java文件中的代码,它扩展了application并生成了全局变量:

import android.app.Application;

public class Global extends Application {
     private int weight;
     private int age;
     private int feet;
     private int inches;


        //START WEIGHT
        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }
        //END WEIGHT


        //START AGE
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
        //END AGE


        //START FEET
        public int getFeet() {
            return feet;
        }

        public void setFeet(int feet) {
            this.feet = feet;
        }
        //END FEET


        //START INCHES
        public int getInches() {
            return inches;
        }

        public void setInches(int inches) {
            this.inches = inches;
        }
        //END INCHES
}
当我尝试在模拟器中运行程序时,程序崩溃。我查看了LogCat,在exercise.java中的这一行中找到了使用断点的
NullPointerException

((Global) this.getApplication()).setWeight(Integer.parseInt(weightField.getText().toString()));

谢谢~:)

如果您想在整个应用程序中保留状态,我强烈建议您查看
SharedReferences
系统

通过应用程序中的任何活动或服务,您可以通过以下操作访问共享首选项:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

您还可以轻松地进行
首选项活动
来更改设置,而无需编写太多代码。

空指针异常的一般提示

  • 在Eclipse中打开调试透视图
  • 选择“断点”视图选项卡
  • 选择“添加Java异常断点”并选择 NullPointerException
  • 通过使用“调试为…”或附加 调试器将通过DDMS连接到正在运行的实例
  • 执行有问题的工作流。那条线会断的 导致了NPE
    谢谢!它似乎对我有用:)不幸的是,我可能不得不重新编写所有的代码lol。但是,是的,我会研究它:D
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);