Java空指针异常(点击)

时间:2013-02-19 13:06:37

标签: java android onclick

我正在尝试通过在字段中输入金额,在微调器中选择from currency,在另一个微调器中选择to currency来创建美元,英镑和欧元的简单货币转换器。然后我在文本字段中输出转换后的金额。

但由于某种原因,我无法超越onClick方法,我不知道为什么。它可能很简单,但我看不到它。

这是我的代码:

 public class Calculate extends Activity implements OnItemSelectedListener,
    OnClickListener {

Button Calculate;
String stringAmount;
double exchangeRate, E, D, Answer;
EditText Amount;
Spinner spinner, spinner1;
int ConvertFrom, ConvertTo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculate);
    Initilise();
    // this is calling the method initilise.
    // listens for the button to be clicked
    Calculate.setOnClickListener(this);

}

private void Initilise() {
    // Method initilise.
    /* Button */Calculate = (Button) findViewById(R.id.cal);
    /* EditText */Amount = (EditText) findViewById(R.id.amount);
    spinner = (Spinner) findViewById(R.id.Currency);
    spinner1 = (Spinner) findViewById(R.id.Currency1);

}

public void format() {

    ConvertFrom = 0;
    ConvertTo = 0;

    stringAmount = Amount.getText().toString();
    E = 0; // set it to 0 as the default
    try {
        E = Integer.parseInt(stringAmount);
    } catch (NumberFormatException e) {

    }
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    spinner();
    switch (v.getId()) {
    case R.id.cal:

        if (ConvertFrom == 0) {

            FromEuro();
        } else if (ConvertFrom == 1) {

            FromUsd();
        } else if (ConvertFrom == 2) {

            FromGbp();
        } else {
            // Amount.setText("Invalid Input");
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .create();
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Input a amount");
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();

        }
    }
}

public void spinner() {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, R.array.Currencies);

    spinner = (Spinner) findViewById(R.id.Currency);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    spinner1 = (Spinner) findViewById(R.id.Currency1);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    int posistion = spinner.getSelectedItemPosition();
    ConvertFrom = posistion;
    int posistion1 = spinner1.getSelectedItemPosition();
    ConvertTo = posistion1;
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

@SuppressWarnings("deprecation")
private void FromEuro() {
    // TODO Auto-generated method stub
    if (ConvertTo == 1) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 2) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 0) {
        AlertDialog alertDialog = new AlertDialog.Builder(null).create();
        alertDialog.setTitle("Input");
        alertDialog.setMessage("Please Select a gender");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // here you can add functions

            }
        });
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
    }
}

@SuppressWarnings("deprecation")
private void FromUsd() {
    // TODO Auto-generated method stub
    if (ConvertTo == 0) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 1) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else {
        if (ConvertTo == 2) {
            AlertDialog alertDialog = new AlertDialog.Builder(null)
                    .create();
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Select a gender");
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();
        }
    }
}

private void FromGbp() {
    // TODO Auto-generated method stub
    if (ConvertTo == 2) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 0) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else {
        if (ConvertTo == 1) {
            Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Select a gender");
            alertDialog.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();
        }
    }
}`

}

但我现在正在接收

02-19 16:55:01.514:E / AndroidRuntime(28638):在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)

以上错误。


此前此问题出现以下错误:

想知道是否有人可以提供帮助。我得到这些错误:

FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.travelcurrencyconverter.Calculate.onClick(Calculate.java:64)
    at android.view.View.performClick(View.java:4222)
    at android.view.View$PerformClick.run(View.java:17273)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4895)
    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:994)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您正尝试访问第64行null的引用成员(点前部分)。

此外,当您发布代码时,将所有内容缩进四个空格以使其全部语法变为彩色。

此外,在执行此操作之前,请确保在IDE中自动格式化代码。