我試圖將雙數據類型的變量轉換爲字符串,反之亦然,但我遇到了由logcat產生的錯誤。請幫我找到並糾正錯誤。雙字符串轉換錯誤
JavaCode:
public class DataTypeConversion extends ActionBarActivity {
TextView stringValue;
TextView numericValue;
double var;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_type_conversion);
stringValue = (TextView) findViewById(R.id.stringValue);
numericValue = (TextView) findViewById(R.id.numericValue);
stringValue.setText("0123.45");
var = Double.valueOf((String) stringValue.getText());
text = String.valueOf(var);
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="com.example.datatypeconversion.DataTypeConversion$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stringValue"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numericValue"/>
logcat的輸出:
04-16 09:17:20.715: D/AndroidRuntime(1056): Shutting down VM
04-16 09:17:20.715: W/dalvikvm(1056): threadid=1: thread exiting with uncaught exception (group=0x41465700)
04-16 09:17:20.735: E/AndroidRuntime(1056): FATAL EXCEPTION: main
04-16 09:17:20.735: E/AndroidRuntime(1056): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datatypeconversion/com.example.datatypeconversion.DataTypeConversion}: java.lang.NullPointerException
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.os.Looper.loop(Looper.java:137)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-16 09:17:20.735: E/AndroidRuntime(1056): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 09:17:20.735: E/AndroidRuntime(1056): at java.lang.reflect.Method.invoke(Method.java:525)
04-16 09:17:20.735: E/AndroidRuntime(1056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-16 09:17:20.735: E/AndroidRuntime(1056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 09:17:20.735: E/AndroidRuntime(1056): at dalvik.system.NativeStart.main(Native Method)
04-16 09:17:20.735: E/AndroidRuntime(1056): Caused by: java.lang.NullPointerException
04-16 09:17:20.735: E/AndroidRuntime(1056): at com.example.datatypeconversion.DataTypeConversion.onCreate(DataTypeConversion.java:31)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.Activity.performCreate(Activity.java:5133)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-16 09:17:20.735: E/AndroidRuntime(1056): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-16 09:17:20.735: E/AndroidRuntime(1056): ... 11 more
04-16 09:17:24.825: I/Process(1056): Sending signal. PID: 1056 SIG: 9
在這行你得到'NPE'? –
@Spring:什麼是NPE? – user3533250
@pearson:我發佈了 – user3533250