【问题标题】:Double Value from textview来自 textview 的双倍值
【发布时间】:2013-02-15 11:07:05
【问题描述】:

我很确定这里有很多类似的问题,但没有一个能帮助我解决这个问题。

假设我有一个主菜单、一个仪表板布局和 4 个按钮。 A,B,C,D

当我按 A 时,我正在开始新的活动 A。在这个活动 A 中,有一个链接,我正在从中获取 json 项目。它在正常的 URL 上工作正常,但是当我试图从编辑文本/文本视图中获取价值时它崩溃了。

 String url = "http://webservice.xxx.com/webservice/getLocationList.php?lat="+ latValue +"&lng="+ lngValue +"";

latValuelngValue 来自我的 R.id.test 中的编辑框,这是 EditText 的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/test"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<EditText
    android:id="@+id/latValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:text="54.9933628" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/lngValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="9.8595655"
    android:inputType="numberDecimal" /> 
    </LinearLayout>

在这里,我想从中获取价值并将其放入链接中。

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


                  EditText lat_source = (EditText)findViewById(R.id.latValue);
here crash -->    Double sourceLat = Double.parseDouble(lat_source.getText().toString());


    EditText lng_source = (EditText)findViewById(R.id.lngValue);
    Double sourceLng = Double.parseDouble(lng_source.getText().toString());


    String url = "http://webservice.xxx.com/webservice/getLocationList.php?lat="+ sourceLat +"&lng="+ sourceLng +"";

    Log.d(TAG, "My URL is = " + url); 

日志

    02-15 11:37:41.826: E/AndroidRuntime(14267):    at android.os.Looper.loop(Looper.java:137)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at android.app.ActivityThread.main(ActivityThread.java:4898)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at dalvik.system.NativeStart.main(Native Method)
    02-15 11:37:41.826: E/AndroidRuntime(14267): Caused by: java.lang.NullPointerException
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at com.androidhive.jsonparsing.LocationBased.onCreate(LocationBased.java:69)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at android.app.Activity.performCreate(Activity.java:5206)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    02-15 11:37:41.826: E/AndroidRuntime(14267):    at android.app.ActivityThread.performLaunchActivity

【问题讨论】:

  • R.layout.main 是包含两个编辑文本的布局吗?
  • 将 Double 数据类型更改为 double 数据类型。
  • 将其转换为double有什么用,直接作为字符串传递给url。
  • 不,不是,它的 R.layout.test
  • 那么你应该使用 R.layout.test 或充气它并使用充气器返回的对象以获得 EditText 对象

标签: android xml android-edittext


【解决方案1】:
LayoutInflater inflater  = LayoutInflater.from(this);
LinearLayout testLayout = (LinearLayout)inflater.inflate(R.layout.test, null)
EditText lat_source = (EditText)testLayout.findViewById(R.id.latValue);
Double sourceLat = Double.parseDouble(lat_source.getText().toString());

如前所述,为了通过findViewById 获取EditText 对象,您的视图应该通过setContentView 添加到视图层次结构中,或者您可以使用充气器并使用充气器返回的视图对象获取EditText

【讨论】:

  • 非常感谢@blackbelt,它也按预期工作。我不知道这个充气机的东西,但现在我有一个解决方案,我明白发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2015-05-24
  • 2014-02-28
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多