不幸的是应用已停止,转换器应用

时间:2015-01-27 21:24:33

标签: android

我正在练习制作这个转换器应用程序,它是一个简单的转换器但是当我在手机中启动我的应用程序时它会说"不幸的是," app"已停止"。该转换器基于关于制作转换器的Vogella Android教程。我自己刚刚添加了一些东西。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text; 

import java.text.DecimalFormat;


public class MainActivity extends ActionBarActivity {
TextView dollarText;
TextView euroText;
EditText enterText;
EditText result;
RadioButton dollarRadio;
RadioButton euroRadio;
RadioButton deci;
ImageButton imgB;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    enterText = (EditText)findViewById(R.id.enterNumbers);
    result = (EditText)findViewById(R.id.result);
    RadioGroup newRadio = (RadioGroup)findViewById(R.id.radioGroup);
    euroText = (TextView)findViewById(R.id.euroTextView);
    dollarText = (TextView)findViewById(R.id.dollarTextView);
    imgB = (ImageButton)findViewById(R.id.buttonConvert);
    imgB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.buttonConvert:
                    dollarRadio = (RadioButton)findViewById(R.id.radioButtonLek);
                    euroRadio = (RadioButton)findViewById(R.id.radioButtonEuro);
                    if(enterText.getText().length() == 0){
                        Toast.makeText(v.getContext(),"Please enter numbers",Toast.LENGTH_SHORT).show();
                    }
                    double userInput = Double.parseDouble(enterText.getText().toString());
                    if(dollarRadio.isChecked()){
                        result.setText(String.valueOf(convertToDollar(userInput)));
                        dollarRadio.setChecked(true);
                        euroRadio.setChecked(false);
                    }else{
                        result.setText(String.valueOf(convertToEuro(userInput)));
                        dollarRadio.setChecked(false);
                        euroRadio.setChecked(true);
                    }
                    break;
            }
        }
    });

    newRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(dollarRadio.isChecked()){
                euroText.setText("Dollar");
                dollarText.setText("Euro");
            }else{
                euroText.setText("Euro");
                dollarText.setText("Dollar");
            }
        }
    });

    deci.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            DecimalFormat newDeci = new DecimalFormat("0.##");
            String s = newDeci.format(result);
            result.setText(s);
        }
    });
}
public static double convertToDollar(double euro){
    return euro / 0.88;
}
public static double convertToEuro(double dollar){
    return dollar * 0.88;
}
}

日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kushtrimmhconverter.converter/com.example.kushtrimmhconverter.converter.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.kushtrimmhconverter.converter.MainActivity.onCreate(MainActivity.java:78)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)

...

 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.kushtrimmhconverter.converter.MainActivity.onCreate(MainActivity.java:78)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

deci永远不会被初始化,因此它是null,所以NullPointerException在调用deci.setOnCheckedChangeListener

答案 1 :(得分:1)

您正在使用RadioButton deci而未对其进行初始化,这就是您的班级投掷NPE的原因

onCreate()方法

中添加此内容
deci = (RadioButton) findViewById(R.id.deci); // change the id as per your layout

另外,您应该将这些行放在onCreate()中。可能不是现在,但将来可能会给你带来同样的麻烦。除非你在点击按钮时看到它(虽然我在你的代码中看不到它)。