【问题标题】:app keeps crashing应用程序不断崩溃
【发布时间】:2012-03-10 18:57:52
【问题描述】:

嘿,我是编程新手,所以我想我会做一个简单的 c 到 f 转换器,但在我打开它之前它一直在模拟器上崩溃,有人知道为什么吗?我知道代码可能都错了,但这是我能想到的唯一方法

package com.jamie.convert;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ConvertorActivity extends Activity implements OnClickListener {

    TextView textout1;
    EditText textIn1;
    Button convert;
    int x=Integer.parseInt(textIn1.getText().toString());
    double fahrenheit = 1.8*x;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        convert = (Button) findViewById(R.id.convert);
        textIn1=(EditText) findViewById(R.id.input);
        textout1=(TextView) findViewById(R.id.output);
        convert.setOnClickListener((android.view.View.OnClickListener)this);

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.convert) {
            textout1.setText(""+fahrenheit);
        }


    }
}

【问题讨论】:

  • 对于任何人开始帮助你,你必须转储你的崩溃输出的 logcat...
  • 您的错误可能在您的布局文件中。
  • 这个问题和javascript有什么关系?为什么是 javascript 标签?
  • 您是否尝试过仅在 Android 开发者网站上进行一些分步教程。这可能是比直接跳入临时工更好的方法。转换器。
  • 即使你修复了崩溃,你仍然得到错误的公式! F= C x 1.8 +32

标签: java android android-emulator


【解决方案1】:

用这个代码代替这个你会得到答案

TextView textout1;
EditText textIn1;
Button convert;
static int x;
//int x=Integer.parseInt(textIn1.getText().toString());
//double fahrenheit = 1.8*x;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    convert = (Button) findViewById(R.id.button1);
    textIn1=(EditText) findViewById(R.id.editText1);
    textout1=(TextView) findViewById(R.id.textView1);
    convert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            x=Integer.parseInt(textIn1.getText().toString());
            double fahrenheit = 1.8*x;
            textout1.setText(""+fahrenheit);
        }
    });

}

这会发生,因为您的 int x 不会从编辑文本中获得任何值。

【讨论】:

    【解决方案2】:
    int x=Integer.parseInt(textIn1.getText().toString());
    

    上述行将在Activity 启动后立即执行。此时,textIn1 将变为 null

    而且这条线不应该以这种方式存在......

    double fahrenheit = 1.8*x;
    

    将这些行替换为...

    int x;
    double fahrenheit;
    

    ...然后将onClick()方法改为...

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.convert) {
            x = Integer.parseInt(textIn1.getText().toString());
            fahrenheit = x * 1.8 + 32
            textout1.setText(String.valueOf(fahrenheit));
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试String.valueOf(fahrenheit); 而不是你的 onClick 中的华氏度

      也移动你的

      int x=Integer.parseInt(textIn1.getText().toString());
      

      到 onclick 方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-26
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        相关资源
        最近更新 更多