【问题标题】:NullPointerException when changing Activities更改活动时出现 NullPointerException
【发布时间】:2011-03-13 19:22:28
【问题描述】:

我是 Android 开发的新手,我有一个关于 NullPointerException 的快速问题。当从第一个活动页面按下按钮时,它会切换到第二个活动,其中有一个NullPointerException。 以下是 main.xml 的“下一步”按钮(用于切换活动的按钮)的代码:

<Button
            android:id="@+id/ButtonNext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:onClick="next">
        </Button>

这是第一个活动的代码:

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);
        }   

在练习类中,我将 ContentView 设置为练习.xml。 在接下来的几个语句中,我想将全局整数权重设置为用户在 EditTextWeight EditText 框中输入的数字。 下面是 Global.java 文件中的代码,它扩展了 Appllication 并生成了全局变量:

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,发现 NullPointerException 在 exercise.java 的这一行中使用断点:

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

谢谢~:)

【问题讨论】:

    标签: android button android-activity nullpointerexception


    【解决方案1】:

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

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

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    

    您还可以轻松地创建PreferencesActivity 来更改设置,而无需编写太多代码。

    【讨论】:

    • 非常感谢!看起来它对我有用:) 不幸的是,我可能不得不重新编写所有代码哈哈。但是,是的,我会调查一下:D
    【解决方案2】:

    空指针异常的一般提示。

    1. 在 Eclipse 中打开 Debug 透视图
    2. 选择“断点”视图选项卡。
    3. 选择“添加 Java 异常断点”并选择 NullPointerException。
    4. 通过使用“调试为...”或附加 通过 DDMS 将调试器连接到正在运行的实例。
    5. 执行有问题的工作流。它会打破这一行 导致NPE。

    【讨论】:

      猜你喜欢
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多