android.content.res.Resources $ NotFoundException:在主字符串资源ID致命异常 [英] android.content.res.Resources$NotFoundException: String resource ID Fatal Exception in Main

查看:151
本文介绍了android.content.res.Resources $ NotFoundException:在主字符串资源ID致命异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图使该取一个小的随机数,并显示在一个TextView用户一个简单的程序。之后终于等到了随机数生成(我认为)程序抛出每当我运行一个致命的异常。在code中没有错误,但我是一个完整的新手,我开始简单的,这样我可以学习。几个小时后我已经提交给寻求帮助。我几乎可以肯定,我的代码片段随机数是在错误的地方,我只是我不知道放在哪里。我到处都试图抛出了同样的错误。

这是的.java

 包com.eai.vgp;

导入了java.util.Random;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.TextView;

公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;

    }


    无规PP =新的随机();
INT A1 = pp.nextInt(10);

TextView的电视=(TextView中)findViewById(R.id.tv); {

tv.setText(A1);}

    }
 

中的XML

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>

<的TextView
    机器人:ID =@ + ID /电视
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真/>

< / RelativeLayout的>
 

LogCat中

  03-03 16:34:25.313:I /处理(740):发送信号。 PID:740 SIG:9
03-03 16:35:02.212:E /跟踪(806):错误打开跟踪文件:没有这样的文件或目录(2)
03-03 16:35:02.802:W /的ResourceType(806):没有包装标识的资源数量00000001获得价值时,
03-03 16:35:02.813:D / AndroidRuntime(806):关闭虚拟机
03-03 16:35:02.813:W / dalvikvm(806):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a13300)
03-03 16:35:02.833:E / AndroidRuntime(806):致命异常:主要
03-03 16:35:02.833:E / AndroidRuntime(806):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.eai.vgp / com.eai.vgp.MainActivity}:android.content.res.Resources $ NotFoundException:字符串资源编号为0x1
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.os.Handler.dispatchMessage(Handler.java:99)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.os.Looper.loop(Looper.java:137)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 16:35:02.833:E / AndroidRuntime(806):在java.lang.reflect.Method.invokeNative(本机方法)
03-03 16:35:02.833:E / AndroidRuntime(806):在java.lang.reflect.Method.invoke(Method.java:511)
03-03 16:35:02.833:E / AndroidRuntime(806):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 16:35:02.833:E / AndroidRuntime(806):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 16:35:02.833:E / AndroidRuntime(806):在dalvik.system.NativeStart.main(本机方法)
03-03 16:35:02.833:E / AndroidRuntime(806):android.content.res.Resources $ NotFoundException:产生的原因字符串资源编号为0x1
03-03 16:35:02.833:E / AndroidRuntime(806):在android.content.res.Resources.getText(Resources.java:229)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.widget.TextView.setText(TextView.java:3620)
03-03 16:35:02.833:E / AndroidRuntime(806):在com.eai.vgp.MainActivity.onCreate(MainActivity.java:22)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.Activity.performCreate(Activity.java:5008)
03-03 16:35:02.833:E / AndroidRuntime(806):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-03 16:35:02.833:E / AndroidRuntime(806):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-03 16:35:02.833:E / AndroidRuntime(806):11 ...更多
03-03 16:35:05.113:I /处理(806):发送信号。 PID:806 SIG:9
 

解决方案

移动

 无规PP =新的随机();
INT A1 = pp.nextInt(10);

TextView的电视=(TextView中)findViewById(R.id.tv); {

tv.setText(A1);}
 

要在的onCreate(),并更改 tv.setText(A1); tv.setText(将String.valueOf(A1));

  @覆盖
保护无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.activity_main);

  无规PP =新的随机();
  INT A1 = pp.nextInt(10);

  TextView的电视=(TextView中)findViewById(R.id.tv);

  tv.setText(将String.valueOf(A1));

}
 

首先的问题: findViewById()的onCreate被称为(),这将抛出一个NPE

第二个问题:直接传递一个int到一个TextView调用重载的方法查找字符串资源(从 R.string )。因此,我们要使用将String.valueOf()强制字符串重载的方法。

I've been trying to make a simple program that fetches a small random number and displays it to the user in a textview. After finally getting the random number to generate (I think) the program throws a fatal exception whenever I run. No errors in code, but I'm a complete newbie and I am starting simple so that I may learn. After hours I've submitted to asking for help. I'm almost certain that my snippet for random numbers is in the wrong area, I just am not sure where to put it. Everywhere I tried throws the same error.

This is the .java

package com.eai.vgp;

import java.util.Random;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }


    Random pp = new Random();
int a1 = pp.nextInt(10);

TextView tv = (TextView)findViewById(R.id.tv);{

tv.setText(a1);}

    }

The 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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

LogCat

03-03 16:34:25.313: I/Process(740): Sending signal. PID: 740 SIG: 9
03-03 16:35:02.212: E/Trace(806): error opening trace file: No such file or directory     (2)
03-03 16:35:02.802: W/ResourceType(806): No package identifier when getting value for     resource number 0x00000001
03-03 16:35:02.813: D/AndroidRuntime(806): Shutting down VM
03-03 16:35:02.813: W/dalvikvm(806): threadid=1: thread exiting with uncaught exception     (group=0x40a13300)
03-03 16:35:02.833: E/AndroidRuntime(806): FATAL EXCEPTION: main
03-03 16:35:02.833: E/AndroidRuntime(806): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.eai.vgp/com.eai.vgp.MainActivity}:     android.content.res.Resources$NotFoundException: String resource ID #0x1
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.access$600(ActivityThread.java:130)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.os.Handler.dispatchMessage(Handler.java:99)
03-03 16:35:02.833: E/AndroidRuntime(806):  at android.os.Looper.loop(Looper.java:137)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     java.lang.reflect.Method.invokeNative(Native Method)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     java.lang.reflect.Method.invoke(Method.java:511)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 16:35:02.833: E/AndroidRuntime(806):  at dalvik.system.NativeStart.main(Native     Method)
03-03 16:35:02.833: E/AndroidRuntime(806): Caused by:     android.content.res.Resources$NotFoundException: String resource ID #0x1
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.content.res.Resources.getText(Resources.java:229)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.widget.TextView.setText(TextView.java:3620)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.eai.vgp.MainActivity.onCreate(MainActivity.java:22)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.Activity.performCreate(Activity.java:5008)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-03 16:35:02.833: E/AndroidRuntime(806):      atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-03 16:35:02.833: E/AndroidRuntime(806):  ... 11 more
03-03 16:35:05.113: I/Process(806): Sending signal. PID: 806 SIG: 9

解决方案

Move

Random pp = new Random();
int a1 = pp.nextInt(10);

TextView tv = (TextView)findViewById(R.id.tv);{

tv.setText(a1);}

To inside onCreate(), and change tv.setText(a1); to tv.setText(String.valueOf(a1)); :

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Random pp = new Random();
  int a1 = pp.nextInt(10);

  TextView tv = (TextView)findViewById(R.id.tv);

  tv.setText(String.valueOf(a1));

}   

First issue: findViewById() was called before onCreate(), which would throw an NPE.

Second issue: Passing an int directly to a TextView calls the overloaded method that looks for a String resource (from R.string). Therefore, we want to use String.valueOf() to force the String overloaded method.

这篇关于android.content.res.Resources $ NotFoundException:在主字符串资源ID致命异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆