2014-04-16 94 views
-2

我試圖將雙數據類型的變量轉換爲字符串,反之亦然,但我遇到了由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 
+0

在這行你得到'NPE'? –

+0

@Spring:什麼是NPE? – user3533250

+0

@pearson:我發佈了 – user3533250

回答

1

你的程序在這一行崩潰:

stringValue.setText("0123.45"); 

這意味着,這條線返回null:

stringValue = (TextView) findViewById(R.id.stringValue); 

的最可能的罪魁禍首是,你不必在activity_data_type_conversion.xml的ID stringValue的,這是你的佈局一個TextView。確保文本視圖存在,並有正確的ID值,它應該工作。

0

試試這個請,我希望它可以幫助你

var = Double.valueOf(stringValue.getText().toString()); 
+0

你確實意識到/這個答案已經給出b /字符串轉換是無用的嗎? – njzk2

+1

不''toString()'總是返回一個字符串? – CodeCamper

+0

我想是的,因爲getText返回字符序列,我們必須將其轉換爲字符串 –