Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行测试android应用程序时出现的几个错误_Java_Android_Xml_String_Runtime Error - Fatal编程技术网

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

Java 运行测试android应用程序时出现的几个错误,java,android,xml,string,runtime-error,Java,Android,Xml,String,Runtime Error,我正试图从一本android开发书籍中测试一个简单的android应用程序,当运行该应用程序时,我收到一条错误消息。不幸的是,应用程序已停止。 (当我取出setStarUpScreenText()时,应用程序运行,但所需的文本不会出现。 主要活动 package chaper.two.hello_world; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import andro

我正试图从一本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>
在用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

根据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
中的atribute
planetMass
所指向的字符串


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

根据错误,您是否确保所有文本视图的名称正确?或者删除R.java和“文件->使缓存无效”如果您使用的是IDEA/Studio。谢谢您的建议。我删除了对earth.planetmass的引用并放置了“Test”相反,我还是出现了一个错误。我将生成的日志放在了帖子中。这听起来确实像是资源文件中的错误。如果还没有,你应该尝试重新生成它。除此之外,我没有ideias。别担心我更改了错误的字符串。它现在可以工作了。谢谢,我在这上面停留了一段时间。谢谢。
planetMassValue.setText(earth.planetMass);