运行测试android应用程序时的几个错误

时间:2013-10-14 19:37:21

标签: java android xml string runtime-error

我正在尝试从Android开发书中测试一个简单的Android应用程序,我在运行应用程序时遇到了一个错误消息。不幸的是应用已经停止 (当我取出setStarUpScreenText();应用程序运行但不显示所需的文本。 MainActivity

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

用test替换earth.planetMass

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

1 个答案:

答案 0 :(得分: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似乎缺少来自类planetMass的对象earth的属性WorldGen所指向的字符串。

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