【问题标题】:Several errors when running testing android application运行测试android应用程序时出现几个错误
【发布时间】:2013-10-14 19:37:21
【问题描述】:

我正在尝试从一本 android 开发书籍中测试一个简单的 android 应用程序,但我在运行该应用程序时遇到了一个错误消息。不幸的是应用程序已停止。 (当我取出 setStarUpScreenText(); 应用程序运行但所需的文本没有出现。 主活动

package chaper.two.hello_world;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    WorldGen earth = new WorldGen("Earth", 5973, 9.78);       //Create a World Object 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        setStartUpWorldValues();
        setStarUpScreenText(); //when this is removed, application runs but text from object is not displayed.
    }
    protected void setStartUpWorldValues(){
        earth.setPlanetColonies(1);                      //Set Planet Colonies to one
        earth.setPlanetMilitary(1);                      //Set Planet Military Bases to on
        earth.setColonyImmigration(1000);                //Set Planet Population to 1,000
        earth.setBaseProtection(100);                    //Set  Planet Armed Forces to 100
        earth.turnForceFieldOn();                        // Turn On the Planet Forcefield
    }
    protected void setStarUpScreenText(){
        //sets up text for each View
        TextView planetNameValue = (TextView)findViewById(R.id.DataView1);
        planetNameValue.setText(earth.planetName);
        TextView planetMassValue = (TextView)findViewById(R.id.DataView2);
        planetMassValue.setText(earth.planetMass);
        TextView planetGravityValue = (TextView)findViewById(R.id.DataView3);
        planetGravityValue.setText(String.valueOf(earth.planetGravity));
        TextView planetColoniesValue = (TextView)findViewById(R.id.DataView4);
        planetColoniesValue.setText(String.valueOf(earth.planetColonies));
        TextView planetPopulationValue = (TextView)findViewById(R.id.DataView5);
        planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
        TextView planetMilitaryValue = (TextView)findViewById(R.id.DataView6);
        planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
        TextView planetBasesValue = (TextView)findViewById(R.id.DataView7);
        planetBasesValue.setText(String.valueOf(earth.planetBases));
        TextView planetForceFieldValue = (TextView)findViewById(R.id.DataView8);
        planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

MainActiviy 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=".MainActivity" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_name_label" />
    <TextView
        android:id="@+id/TextView2"
        android:layout_below="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_mass_label" />
    <TextView
        android:id="@+id/TextView3"
        android:layout_below="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_gravity_label" />
    <TextView
        android:id="@+id/TextView4"
        android:layout_below="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_colonies_label" />
    <TextView
        android:id="@+id/TextView5"
        android:layout_below="@+id/TextView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_population_label" />
    <TextView
        android:id="@+id/TextView6"
        android:layout_below="@+id/TextView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_military_label" />   
    <TextView
        android:id="@+id/TextView7"
        android:layout_below="@+id/TextView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_bases_label" />
    <TextView
        android:id="@+id/TextView8"
        android:layout_below="@+id/TextView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_forcefield_label" />   
    <TextView
        android:id="@+id/DataView1"
        android:layout_toRightOf="@+id/TextView1"
        android:layout_marginLeft="36dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
    <TextView 
        android:id="@+id/DataView2"
        android:layout_toRightOf="@+id/TextView2"
        android:layout_below="@+id/DataView1"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/DataView3"
        android:layout_toRightOf="@+id/TextView3"
        android:layout_below="@+id/DataView2"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/DataView4"
        android:layout_toRightOf="@+id/TextView4"
        android:layout_below="@+id/DataView3"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/DataView5"
        android:layout_toRightOf="@+id/TextView5"
        android:layout_below="@+id/DataView4"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/DataView6"
        android:layout_toRightOf="@+id/TextView6"
        android:layout_below="@+id/DataView5"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/DataView7"
        android:layout_toRightOf="@+id/TextView6"
        android:layout_below="@+id/DataView6"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/DataView8"
        android:layout_toRightOf="@+id/TextView8"
        android:layout_below="@+id/DataView7"
        android:layout_alignStart="@+id/DataView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

我的 logcat 错误:

10-14 04:16:58.866: E/AndroidRuntime(853): FATAL EXCEPTION: main
10-14 04:16:58.866: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to start activity ComponentInfo{chaper.two.hello_world/chaper.two.hello_world.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:137)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-14 04:16:58.866: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 04:16:58.866: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:525)
10-14 04:16:58.866: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-14 04:16:58.866: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 04:16:58.866: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
10-14 04:16:58.866: E/AndroidRuntime(853): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.content.res.Resources.getText(Resources.java:239)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.widget.TextView.setText(TextView.java:3837)
10-14 04:16:58.866: E/AndroidRuntime(853):  at chaper.two.hello_world.MainActivity.setStarUpScreenText(MainActivity.java:28)
10-14 04:16:58.866: E/AndroidRuntime(853):  at chaper.two.hello_world.MainActivity.onCreate(MainActivity.java:15)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.Activity.performCreate(Activity.java:5133)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 04:16:58.866: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-14 04:16:58.866: E/AndroidRuntime(853):  ... 11 more

将 earth.planetMass 替换为 Test 后

10-14 16:11:03.537: W/ResourceType(827): No package identifier when getting value for resource number 0x00001755
10-14 16:11:03.546: D/AndroidRuntime(827): Shutting down VM
10-14 16:11:03.546: W/dalvikvm(827): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-14 16:11:03.556: E/AndroidRuntime(827): FATAL EXCEPTION: main
10-14 16:11:03.556: E/AndroidRuntime(827): java.lang.RuntimeException: Unable to start activity ComponentInfo{chaper.two.hello_world/chaper.two.hello_world.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:137)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-14 16:11:03.556: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:11:03.556: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:525)
10-14 16:11:03.556: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-14 16:11:03.556: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 16:11:03.556: E/AndroidRuntime(827):  at dalvik.system.NativeStart.main(Native Method)
10-14 16:11:03.556: E/AndroidRuntime(827): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.content.res.Resources.getText(Resources.java:239)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.widget.TextView.setText(TextView.java:3837)
10-14 16:11:03.556: E/AndroidRuntime(827):  at chaper.two.hello_world.MainActivity.setStarUpScreenText(MainActivity.java:29)
10-14 16:11:03.556: E/AndroidRuntime(827):  at chaper.two.hello_world.MainActivity.onCreate(MainActivity.java:15)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.Activity.performCreate(Activity.java:5133)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 16:11:03.556: E/AndroidRuntime(827):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-14 16:11:03.556: E/AndroidRuntime(827):  ... 11 more
10-14 16:11:16.206: I/Process(827): Sending signal. PID: 827 SIG: 9
10-14 16:11:40.146: W/ResourceType(853): No package identifier when getting value for resource number 0x00001755
10-14 16:11:40.146: D/AndroidRuntime(853): Shutting down VM
10-14 16:11:40.146: W/dalvikvm(853): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-14 16:11:40.166: E/AndroidRuntime(853): FATAL EXCEPTION: main
10-14 16:11:40.166: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to start activity ComponentInfo{chaper.two.hello_world/chaper.two.hello_world.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:137)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-14 16:11:40.166: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:11:40.166: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:525)
10-14 16:11:40.166: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-14 16:11:40.166: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 16:11:40.166: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
10-14 16:11:40.166: E/AndroidRuntime(853): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.content.res.Resources.getText(Resources.java:239)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.widget.TextView.setText(TextView.java:3837)
10-14 16:11:40.166: E/AndroidRuntime(853):  at chaper.two.hello_world.MainActivity.setStarUpScreenText(MainActivity.java:29)
10-14 16:11:40.166: E/AndroidRuntime(853):  at chaper.two.hello_world.MainActivity.onCreate(MainActivity.java:15)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.Activity.performCreate(Activity.java:5133)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 16:11:40.166: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-14 16:11:40.166: E/AndroidRuntime(853):  ... 11 more
10-14 16:17:27.547: W/ResourceType(941): No package identifier when getting value for resource number 0x00001755
10-14 16:17:27.547: D/AndroidRuntime(941): Shutting down VM
10-14 16:17:27.547: W/dalvikvm(941): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-14 16:17:27.566: E/AndroidRuntime(941): FATAL EXCEPTION: main
10-14 16:17:27.566: E/AndroidRuntime(941): java.lang.RuntimeException: Unable to start activity ComponentInfo{chaper.two.hello_world/chaper.two.hello_world.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.os.Looper.loop(Looper.java:137)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-14 16:17:27.566: E/AndroidRuntime(941):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 16:17:27.566: E/AndroidRuntime(941):  at java.lang.reflect.Method.invoke(Method.java:525)
10-14 16:17:27.566: E/AndroidRuntime(941):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-14 16:17:27.566: E/AndroidRuntime(941):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 16:17:27.566: E/AndroidRuntime(941):  at dalvik.system.NativeStart.main(Native Method)
10-14 16:17:27.566: E/AndroidRuntime(941): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.content.res.Resources.getText(Resources.java:239)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.widget.TextView.setText(TextView.java:3837)
10-14 16:17:27.566: E/AndroidRuntime(941):  at chaper.two.hello_world.MainActivity.setStarUpScreenText(MainActivity.java:29)
10-14 16:17:27.566: E/AndroidRuntime(941):  at chaper.two.hello_world.MainActivity.onCreate(MainActivity.java:15)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.Activity.performCreate(Activity.java:5133)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 16:17:27.566: E/AndroidRuntime(941):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-14 16:17:27.566: E/AndroidRuntime(941):  ... 11 more
10-14 16:17:33.606: I/Process(941): Sending signal. PID: 941 SIG: 9

【问题讨论】:

  • 根据错误,您是否确保所有文本视图都正确命名?

标签: java android xml string runtime-error


【解决方案1】:

根据 LogCat,您缺少引用的字符串资源:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755

在第 28 行

at chaper.two.hello_world.MainActivity.setStarUpScreenText(MainActivity.java:28)

这就是这一行:

planetMassValue.setText(earth.planetMass);

您的res/values/strings.xml 似乎缺少WorldGen 类的对象earth 中的属性planetMass 指向的字符串。

如果字符串存在,并且您正确引用它,则 R 文件可能存在问题。所以只需做一个项目 -> 清理,以便重建 R 文件。

【讨论】:

  • 如果你使用的是 IDEA/Studio,或者删除 R.java 和 "File->Invalidate caches"。
  • 感谢您的建议。我删除了对 earth.planetmass 的引用并改为放置“测试”,但仍然出现错误。我将生成的日志放在帖子中。
  • 这听起来确实像是资源文件中的错误。如果你还没有,你应该尝试重建它。除此之外,我没有想法。
  • 没关系我改错了字符串。现在可以了。谢谢我被困了一段时间。谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
相关资源
最近更新 更多