编译此代码时出错:

时间:2013-11-24 06:19:56

标签: android eclipse

我的应用在执行后崩溃,并显示“错误:NullPointerException”。不知道该怎么办。我正在尝试使用Android SDK在eclipse中制作一个简单的货币转换器。如果有人能告诉我如何制作它,那就太棒了!(Android和eclipse的新手)

package com.example.converter;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  

public class MainActivity extends Activity implements OnClickListener{
EditText et = (EditText) findViewById(R.id.editText1);
EditText et1 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(this);


}

    public void onClick(View v) {
        // TODO Auto-generated method stub

        //EditText edit = (EditText) findViewById(R.id.editText1);
        String value = et.getText().toString(); // get the value from editText
        String result =convertToAED(value);
}

@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 String convertToAED(String value)
{   double input = 0;
    try{
     input = Double.parseDouble(value);
       }catch(NumberFormatException e)
       {
           e.printStackTrace();
       }
    double rate = Double.parseDouble(et1.getText().toString());
    double output = input*rate;
    return output+"";
}

public String convertToUSD(String value)
{try{
     input = Double.parseDouble(value);
       }catch(NumberFormatException e)
       {
           e.printStackTrace();
       }
    double rate = Double.parseDouble(et1.getText().toString());
    double output = input*rate;
    return output+"";
}

}

XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="27dp"
    android:ems="10"
    android:hint="@string/edit" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:prompt="@array/Currency"
    tools:listitem="@android:layout/simple_spinner_dropdown_item" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="121dp"
    android:hint="@string/output"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/spinner1"
    android:hint="Enter rate"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:onClick="onClick"
    android:layout_centerVertical="true"
    android:text="@string/Convert" />

</RelativeLayout>

logcat的:

11-24 01:11:18.720: D/dalvikvm(2030): Not late-enabling CheckJNI (already on)
11-24 01:11:23.960: I/Choreographer(2030): Skipped 484 frames!  The application may be doing too much work on its main thread.
11-24 01:11:24.250: D/gralloc_goldfish(2030): Emulator without GPU emulation detected.
11-24 01:11:33.440: D/AndroidRuntime(2030): Shutting down VM
11-24 01:11:33.440: W/dalvikvm(2030): threadid=1: thread exiting with uncaught exception (group=0xb0f4b648)
11-24 01:11:33.470: E/AndroidRuntime(2030): FATAL EXCEPTION: main
11-24 01:11:33.470: E/AndroidRuntime(2030): java.lang.NumberFormatException: Invalid double: "android.widget.EditText{b11f6860 VFED..CL .F....I. 24,250-456,309 #7f080002 app:id/editText1}"
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.initialParse(StringToReal.java:114)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.Double.parseDouble(Double.java:295)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.example.converterbase.MainActivity.conevrt(MainActivity.java:43)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.example.converterbase.MainActivity$1.onClick(MainActivity.java:27)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.view.View.performClick(View.java:4240)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.view.View$PerformClick.run(View.java:17721)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Handler.handleCallback(Handler.java:730)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Looper.loop(Looper.java:137)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.reflect.Method.invokeNative(Native Method)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.reflect.Method.invoke(Method.java:525)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at dalvik.system.NativeStart.main(Native Method)
11-24 01:11:36.240: I/Process(2030): Sending signal. PID: 2030 SIG: 9

1 个答案:

答案 0 :(得分:1)

更改为

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        EditText edit = (EditText) findViewById(R.id.editText1);
        String value = edit.getText().toString(); // get the value from editText
        String result =conevrt(value); // pass the value as param 

    }

您正在创建一个错误的MainActivity MainActivity ma = new MainActivity()实例。活动有一个生命周期。

另外

http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)

  try
  {
   Double input = Double.parseDouble(value); // numberformatexception if value is not a double value
  }catch(NumberFormatException e)
  {
       e.printStacktrace();
  }