【问题标题】:basic calculator android development failing基本计算器android开发失败
【发布时间】:2013-07-06 09:35:44
【问题描述】:

我正在关注有关创建温度转换器的教程,并且它有效。然后我想让我开发一个计算器(基本的编辑文本、文本视图和按钮操作)来进一步练习使用这些小部件。

我写了下面的代码,但由于某种原因它失败了。我相信在传递给 result.setText() 时它是 NullPointerException,但我什至在那里硬编码了一个字符串,但它不起作用。第一个代码是我的代码,另一个代码是我的代码:

public class MainActivity extends Activity {


    private EditText firstNumber;
    private EditText secondNumber;
    private TextView result;

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

    public void calculate(View view)
    {
        switch(view.getId())
        {
        case R.id.additionButton:
            firstNumber = (EditText) findViewById(R.id.editText1);
            secondNumber = (EditText) findViewById(R.id.editText2);
            if(firstNumber.getText().length() != 0 && secondNumber.getText().length() != 0)
            {
                float x = Float.parseFloat(firstNumber.getText().toString());
                float y = Float.parseFloat(secondNumber.getText().toString());
                result.setText(String.valueOf(addition(x, y)));
            }
         break; 
        }
    }

    private float addition(float x, float y)
    {
        return x + y;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

和转换器代码:

public class MainActivity extends Activity {
      private EditText text;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText1);

      }

      // This method is called at button click because we assigned the name to the
      // "OnClick property" of the button
      public void Convert(View view) {
        switch (view.getId()) {
        case R.id.button1:
          RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
          RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
          if (text.getText().length() == 0) {
            Toast.makeText(this, "Please enter a valid number",
                Toast.LENGTH_LONG).show();
            return;
          }

          float inputValue = Float.parseFloat(text.getText().toString());
          if (celsiusButton.isChecked()) {
            text.setText(String
                .valueOf(convertFahrenheitToCelsius(inputValue)));
            celsiusButton.setChecked(false);
            fahrenheitButton.setChecked(true);
          } else {
            text.setText(String
                .valueOf(convertCelsiusToFahrenheit(inputValue)));
            fahrenheitButton.setChecked(false);
            celsiusButton.setChecked(true);
          }
          break;
        }
      }

      // Converts to celsius
      private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
      }

      // Converts to fahrenheit
      private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
      }
    } 

在我开始 android 开发时请帮帮我:) 非常感谢!

注意:我说我相信是因为我还在习惯调试器。这是它显示的错误消息:

07-06 12:31:39.236: E/AndroidRuntime(28638): 由: java.lang.NullPointerException 07-06 12:31:39.236: E/AndroidRuntime(28638):在 com.khalid.basiccalulator.MainActivity.calculate(MainActivity.java:34)

【问题讨论】:

  • 我相信这是一个 NullPointerException 千万不要相信,你有一个包含所有异常信息的 LogCat,打开它看看是什么导致您的应用崩溃

标签: java android nullpointerexception


【解决方案1】:
private TextView result;

你从未初始化过 TextView 结果。

result = (TextView) findViewById(R.id.idofresulttextview);

idofresulttextview 必须在 activity_main.xml 布局中声明,并且必须是 TextView

【讨论】:

  • 能否详细说明它应该在哪里以及如何初始化?
  • 好伙伴!我忘记了这一切以及为未来发展保留的东西。欣赏它:)
【解决方案2】:

TextView result 未初始化。

在您的onCreate() 方法中的setContentView() 之后,您应该初始化您的result 变量。代码应该是这样的:

result = (TextView) findViewById(R.id.textView);

【讨论】:

  • 能否详细说明它应该在哪里以及如何初始化?
【解决方案3】:

你应该初始化字段result

private TextView result;

result =  ??

【讨论】:

  • 如果我写这个,它会在启动时破坏整个应用程序。除非我写错地方了
  • @TheNewIdiot 我的意图是初始化 ..不仅用新的关键字...见黑带答案..
  • @Baadshah 您使用了在这种情况下不起作用的 new 关键字。如果某事不起作用;那它就不是一个答案。
  • @Baadshah 你应该记得你在回答一个Android问题。
  • @TheNewIdiot 我刚刚提出了未初始化的问题。无论如何感谢兄弟。美好的一天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多