【问题标题】:android.content.res.Resources$NotFoundException: String resource ID #0x12 - onLocationChanged()android.content.res.Resources$NotFoundException:字符串资源 ID #0x12 - onLocationChanged()
【发布时间】:2014-10-14 12:00:55
【问题描述】:

我的代码:

//GeolocationActivity.java

@Override
    public void onLocationChanged(Location arg0) {
        if(gpson&&arg0!=null)
        {
            tv1.setText((int)arg0.getLongitude()); //there is an error
            tv2.setText((int)arg0.getLatitude());
            tv3.setText((int)arg0.getAccuracy());
        }
    }

private void setUpGPS()
    {
        if(locman.isProviderEnabled(LocationManager.GPS_PROVIDER) == false)
        {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
            Toast.makeText(getApplicationContext(), R.string.gps_enabling, Toast.LENGTH_LONG).show();
        }else{
        setContentView(R.layout.subactivity_gps);       gpson=true;
        //some code removed
        tv1 = (TextView)findViewById(R.id.gps_longitude_txt);
        tv2 = (TextView)findViewById(R.id.gps_langitude_txt);
        tv3 = (TextView)findViewById(R.id.gps_accuracy_text);
        Criteria c = new Criteria();
        prov = locman.getBestProvider(c, false);
        Location loc = locman.getLastKnownLocation(prov);
        if(loc!=null)onLocationChanged(loc);
        }
    }
//setUpGPS() is called in onCreate() - there are also other variables initzialized

Logcat:

10-14 13:52:41.575: E/AndroidRuntime(11386): FATAL EXCEPTION: main
10-14 13:52:41.575: E/AndroidRuntime(11386): android.content.res.Resources$NotFoundException: String resource ID #0x12
10-14 13:52:41.575: E/AndroidRuntime(11386):    at android.content.res.Resources.getText(Resources.java:1068)
10-14 13:52:41.575: E/AndroidRuntime(11386):    at android.widget.TextView.setText(TextView.java:4546)
10-14 13:52:41.575: E/AndroidRuntime(11386):    at com.radzik.myapp.GeolocationActivity.onLocationChanged(GeolocationActivity.java:185)

XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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:keepScreenOn="true"
    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.radzik.myapp.GeolocationActivity" >
//some code removed

    <TextView
        android:id="@+id/gps_longitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gps_backmain"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:text="@string/longitude"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gps_longitude_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/gps_longitude"
        android:layout_alignBottom="@+id/gps_longitude"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginLeft="20dp"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gps_langitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gps_longitude"
        android:layout_below="@+id/gps_longitude"
        android:text="@string/langitude"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gps_langitude_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/gps_langitude"
        android:layout_alignBottom="@+id/gps_langitude"
        android:layout_alignLeft="@+id/textView1"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gps_accuracy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gps_langitude"
        android:layout_below="@+id/gps_langitude"
        android:text="@string/gps_accuracy"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gps_accuracy_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gps_langitude_txt"
        android:layout_toRightOf="@+id/gps_longitude_txt"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceMedium" />

 //some code removed

</RelativeLayout>

我不知道这个错误的原因。尝试调试,但没有发现 - 一切都被正确定义。在这种情况下请帮助我

【问题讨论】:

  • 你在哪里打电话给setUpGPS()
  • 刚刚写的 - 在onCreate()

标签: android android-layout runtime-error android-logcat


【解决方案1】:

你给 textview 一个整数值,这样它就会在 R.java 中找到匹配的 id

尝试像这样连接值

@Override
public void onLocationChanged(Location arg0) {
    if(arg0!=null)
    {
        tv1.setText(String.valueOf(arg0.getLongitude())); //there is an error
        tv2.setText(String.valueOf(arg0.getLatitude()));
        tv3.setText(String.valueOf(arg0.getAccuracy()));
    }
}

【讨论】:

  • 它将尝试找到匹配项
  • 好答案。谢谢你。我会在 9 分钟内接受它
  • 不是使用"",我们不能把那个长值传递给String.valueOf(),然后把它传递给setText()吗?
  • SilentKiller 的方式要好很多,也可以使用Integer.toString()
  • 它们是一样的
猜你喜欢
  • 2013-06-27
  • 2021-05-09
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
相关资源
最近更新 更多