Android 编译此代码时出错:
我的应用程序在执行后崩溃,并显示“错误:NullPointerException”。不知道该怎么办。我正在尝试使用Android SDK在eclipse中制作一个简单的货币转换器。如果有人能告诉我怎么做,那就太好了!。(android和eclipse的新手) } XML文件:Android 编译此代码时出错:,android,eclipse,Android,Eclipse,我的应用程序在执行后崩溃,并显示“错误:NullPointerException”。不知道该怎么办。我正在尝试使用Android SDK在eclipse中制作一个简单的货币转换器。如果有人能告诉我怎么做,那就太好了!。(android和eclipse的新手) } XML文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andro
<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>
改为
@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()
,这是错误的。活动具有生命周期
也
它解决了一些问题,但应用程序仍然崩溃11-24 06:27:01.050:E/AndroidRuntime(878):致命异常:主“”11-24 06:27:01.050:E/AndroidRuntime(878):java.lang.RuntimeException:无法实例化活动组件信息{com.example.converter/com.example.converter.MainActivity}:java.lang.NullPointerException'@user3026643导致NPEIt的哪一行显示在这里'11-24 13:06:50.010:E/AndroidRuntime(780):位于dalvik.system.NativeStart.main(本机方法)11-24 13:06:50.010:E/AndroidRuntime(780):由:java.lang.NullPointerException 11-24 13:06:50.010:E/AndroidRuntime(780):位于android.app.Activity.findViewById(Activity.java:1883)@user3026643在初始化视图时检查xml中的ID是否匹配。同时确定导致NPEI通过ID的行,它看起来很好,但对于NPE,我不知道它为什么会显示该错误。
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
@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
}
try
{
Double input = Double.parseDouble(value); // numberformatexception if value is not a double value
}catch(NumberFormatException e)
{
e.printStacktrace();
}