0

I am trying to test a simple android application from an android development book and I reached a point when running the application gets me an error message. Unfortunately app has stopped. (When I take out setStarUpScreenText(); the application runs but the desired text does not appear. 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>

My logcat errors:

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

After replacing earth.planetMass with 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
4

1 回答 1

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缺少classplanetMass的 object中的属性指向的字符串。earthWorldGen

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

于 2013-10-14T19:46:19.520 回答