想做一个程序,动态加载多张图片用textview同时显示,位置的坐标和显示的数字都随机,所以采用了代码布局和absolutelayout。我写的代码如下;package com.passion;import java.util.Random;import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.TextView;public class GameActivity extends Activity {
private TextView[] text;
private AbsoluteLayout.LayoutParams lp[];
private int count;
private DisplayMetrics dm;
int width,height;
private int[] number,locationX,locationY;
private OnClickListener listener;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
AbsoluteLayout layout = new AbsoluteLayout(this);
setContentView(layout);
//设置背景
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.bac);
this.getWindow().setBackgroundDrawable(drawable);

//设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/LithosPro-Bold.ttf");

//获取屏幕的尺寸
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;

count = 3;
text = new TextView[count];
number = randRange(count,10);
locationX = new int[count];
locationX = randRange(count,width-60);
locationY = new int[count];
locationY = randRange(count,height-60);

for(int i = 0;i<count;i++){
text[i] = new TextView(this);
text[i].setTypeface(typeface);
text[i].setText(number[i]);
text[i].setTextSize(60);
lp[i] = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,locationX[i],locationY[i]);
text[i].setLayoutParams(lp[i]);
layout.addView(text[i]);

  }


}
    
//对屏幕坐标取随机值,num是取值的个数,range是取值范围
public int[] randRange(int num,int range){
        Random rand = new Random();
        boolean flag = true;
        int result[] = new int[num];
        result[0] = rand.nextInt(range);
        int count = 1;
        while(flag&&count<num){
         flag = false;
         result[count] = rand.nextInt(range);
         if(result[count]==result[count-1])
         break;
         else{
         count++;
         flag=true;
         }
        }
        return result;
    

    }
}用log.i试了下,错误好像在循环里,但是具体的我搞不清楚,求助各位大神,到底哪里需要改的?不吝赐教了各位,谢谢!!!!

解决方案 »

  1.   

    for(int i = 0;i<count;i++){
    Log.i("log", "loop top");
    text[i] = new TextView(this);
    Log.i("log", "loop line one");
    text[i].setTypeface(typeface);
    Log.i("log", "loop line two");
    text[i].setText(number[i]);
    Log.i("log", "loop line three");
    text[i].setTextSize(60);
    Log.i("log", "loop line four");
    lp[i] = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,locationX[i],locationY[i]);
    Log.i("log", "loop line five");
    text[i].setLayoutParams(lp[i]);
    Log.i("log", "loop line six");
    layout.addView(text[i]);
    Log.i("log", "loop line seven");

      }logcat的显示:
    01-13 04:06:28.648: I/log(372): loop top
    01-13 04:06:28.658: I/log(372): loop line one
    01-13 04:06:28.658: I/log(372): loop line two
    01-13 04:06:28.658: W/ResourceType(372): No package identifier when getting value for resource number 0x00000002
    01-13 04:06:28.678: W/dalvikvm(372): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    01-13 04:06:28.708: E/AndroidRuntime(372): FATAL EXCEPTION: main
    01-13 04:06:28.708: E/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passion/com.passion.GameActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.os.Handler.dispatchMessage(Handler.java:99)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.os.Looper.loop(Looper.java:123)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at java.lang.reflect.Method.invoke(Method.java:507)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at dalvik.system.NativeStart.main(Native Method)
    01-13 04:06:28.708: E/AndroidRuntime(372): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.content.res.Resources.getText(Resources.java:201)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.widget.TextView.setText(TextView.java:2857)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at com.passion.GameActivity.onCreate(GameActivity.java:63)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    01-13 04:06:28.708: E/AndroidRuntime(372):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    01-13 04:06:28.708: E/AndroidRuntime(372):  ... 11 more
    01-13 04:06:35.668: I/Process(372): Sending signal. PID: 372 SIG: 9
      

  2.   

    text[i].setText(number[i]);
    -----------------------------
    setText(int)方法只任resouce id, 而你没有定义对应的resouce所以出错.
    如果你想把随机生成的数字表示出来, 请使用 text[i].setText(String.valueOf(number[i]));
      

  3.   

    谢谢楼上两位,我按照3楼的方法改了,现在程序运行到了
    lp[i] = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,locationX[i],locationY[i]);
    这行又报错了:
    01-13 13:05:33.420: I/log(348): loop top
    01-13 13:05:33.420: I/log(348): loop line one
    01-13 13:05:33.430: I/log(348): loop line two
    01-13 13:05:33.430: I/log(348): loop line three
    01-13 13:05:33.430: I/log(348): loop line four
    01-13 13:05:33.441: D/AndroidRuntime(348): Shutting down VM
    01-13 13:05:33.441: W/dalvikvm(348): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    01-13 13:05:33.460: E/AndroidRuntime(348): FATAL EXCEPTION: main
    ........
    是不是因为一个absolutelayout只能有一个absolutelayout.LayoutParams呢?可是我不给每个textview加上一个的话也没法定位。请继续指教.
      

  4.   

    1、AbsoluteLayout 早被抛弃了,不知道楼主用哪个版本来编译?我用android 2.1 ,IDE都横划一条线叫我不要用了。
    2、请在AddView后,再调用 View.getLayoutParams(),转换成对应的父Layout类型,设置完后,再View.setLayoutParams()回去。例如
    ...
    text[i].setTextSize(60);
    layout.addView(text[i]);
    lp[i] = (AbsoluteLayout.LayoutParams) text[i].getLayoutParams();
    lp[i] ... // do sth. to lp[i]
    text[i].setLayoutParams(lp[i]);因为text[i] 被 AddView() 后,才会产生 Layout 的
      

  5.   

    cenguokai:
    谢谢你,问题解决了。我把layout.addView(text[i])放在前面,然后又抛了空指针异常,我把定义改了:private TextView[] text = new TextView[count],接着抛数组越界,再把count的值在一开始就定义出来:private int count = 5;运行成功!!
    我用的是android 2.3.3编译的,absolutelayout也是划了根线叫我不要用了,不过为了得到精确的坐标位置,还是不得不用啊。