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