这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity2);
Button n = (Button) findViewById(R.id.button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
n.setTypeface(typeface);
final TextView tv = (TextView) findViewById(R.id.textView);
Typeface face = Typeface.createFromAsset(getAssets(),
"OSP-DIN.ttf");
tv.setTypeface(face);
final String[] values = getResources().getStringArray(R.array.things_array);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nextValue = values[RAND.nextInt(values.length)];
tv.setText(nextValue);
}
});
}
在值[RAND,RAND是红色并说它无法解决?
我该如何解决这个问题?
马修
logcat的:
08-13 21:41:48.302 9865-9865 / com.MR.brd E / Trace:错误打开跟踪文件:没有这样的文件或目录(2) 08-13 21:41:48.351 9865-9865 / com.MR.brd V / ActivityThread:类路径:/data/app/com.MR.brd-1.apk,JNI路径:/data/data/com.MR .brd / lib目录 08-13 21:41:48.566 9865-9865 / com.MR.brd D / dalvikvm:GC_FOR_ALLOC释放52K,4%免费8339K / 8643K,暂停51ms,总计65ms 08-13 21:41:48.641 9865-9865 / com.MR.brd I / dalvikvm-heap:将堆(frag case)增长到16.150MB,用于8294416字节分配 08-13 21:41:48.716 9865-9872 / com.MR.brd D / dalvikvm:GC_FOR_ALLOC释放< 1K,3%free 16438K / 16775K,暂停75ms,总计75ms 08-13 21:41:48.813 9865-9870 / com.MR.brd D / dalvikvm:GC_CONCURRENT释放1K,3%释放16438K / 16775K,暂停15ms + 1ms,总计97ms 08-13 21:41:48.948 9865-9865 / com.MR.brd I / SurfaceTextureClient:[void android :: SurfaceTextureClient :: init()] debug.stc.fps:3000 ms 08-13 21:41:48.954 9865-9865 / com.MR.brd D / libEGL:已加载/vendor/lib/egl/libEGL_mtk.so 08-13 21:41:48.978 9865-9865 / com.MR.brd D / libEGL:已加载/vendor/lib/egl/libGLESv1_CM_mtk.so 08-13 21:41:48.982 9865-9865 / com.MR.brd D / libEGL:已加载/vendor/lib/egl/libGLESv2_mtk.so 08-13 21:41:49.170 9865-9865 / com.MR.brd E / MMUMapper:无法注册MVA,格式不受支持(0x5) 08-13 21:41:49.176 9865-9865 / com.MR.brd D / OpenGLRenderer:启用调试模式0 08-13 21:41:49.436 9865-9865 / com.MR.brd E / MMUMapper:未能注册MVA,格式不受支持(0x5) 08-13 21:41:55.045 9865-9865 / com.MR.brd D / VelocityTracker:无法打开' / dev / touch' (没有相应的文件和目录) 08-13 21:41:55.045 9865-9865 / com.MR.brd D / VelocityTracker:tpd读取x失败:文件编号错误 08-13 21:41:55.045 9865-9865 / com.MR.brd D / VelocityTracker:tpd读取失败:文件编号错误 08-13 21:41:55.116 9865-9865 / com.MR.brd V / Provider / Setting:invalidate [system]:current 2613!= cached 0 08-13 21:41:55.119 9865-9865 / com.MR.brd V / Provider / Setting:来自db cache,name = sound_effects_enabled value = 0 08-13 21:41:55.242 9865-9865 / com.MR.brd W / Resources:转换为字符串:TypedValue {t = 0x1 / d = 0x7f050001 a = -1 r = 0x7f050001} 08-13 21:41:55.314 9865-9865 / com.MR.brd I / SurfaceTextureClient:[void android :: SurfaceTextureClient :: init()] debug.stc.fps:3000 ms 08-13 21:41:55.341 9865-9865 / com.MR.brd E / MMUMapper:无法注册MVA,格式不受支持(0x5) 08-13 21:41:55.507 9865-9865 / com.MR.brd E / MMUMapper:无法注册MVA,格式不受支持(0x5) 08-13 21:41:55.547 9865-9865 / com.MR.brd E / MMUMapper:使用VA(0x52d7d000)大小(1536000)取消注册MVA的操作无效 08-13 21:41:55.547 9865-9865 / com.MR.brd E / MMUMapper:使用VA(0x53264000)大小(1536000)取消注册MVA的操作无效 08-13 21:41:55.560 9865-9865 / com.MR.brd I / SurfaceTextureClient:[void android :: SurfaceTextureClient :: init()] debug.stc.fps:3000 ms 08-13 21:41:59.563 9865-9865 / com.MR.brd V / Provider / Setting:from settings cache,name = sound_effects_enabled value = 0 08-13 21:41:59.563 9865-9865 / com.MR.brd W / ResourceType:获取资源号0x00000000的值时没有包标识符 08-13 21:41:59.564 9865-9865 / com.MR.brd D / AndroidRuntime:关闭VM 08-13 21:41:59.564 9865-9865 / com.MR.brd W / dalvikvm:threadid = 1:线程退出未捕获异常(组= 0x410458a8) 08-13 21:41:59.576 9865-9865 / com.MR.brd E / AndroidRuntime:FATAL EXCEPTION:main android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 在android.content.res.Resources.getText(Resources.java:230) 在android.widget.TextView.setText(TextView.java:3856) 在com.MR.brd.MyActivity2 $ 1.onClick(MyActivity2.java:48) 在android.view.View.performClick(View.java:4101) 在android.view.View $ PerformClick.run(View.java:17088) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:153) 在android.app.ActivityThread.main(ActivityThread.java:5071) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method) 08-13 21:42:01.384 9865-9865 / com.MR.brd I / Process:发送信号。 PID:9865 SIG:9
答案 0 :(得分:2)
您错过了定义RAND
Random RAND=new Random();
答案 1 :(得分:0)
你必须这样做:
Random r = new Random();
r.nextInt(values.length);
请参阅:http://www.tutorialspoint.com/java/util/random_nextint_inc_exc.htm
编辑:
ArrayList<Integer> alReadyGet = new ArrayList<Integer>();
Random r = new Random();
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextValue = r.nextInt(values.length);
for (int i =0; i < alReadyGet.size(); i++) {
if (alReadyGet.get(i) == nextValue) {
nextValue = r.nextInt(values.length);
}
}
alReadyGet.add(nextValue);
tv.setText(values[nextValue]);
});
我认为它会起作用:)告诉我:p