Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 试图从另一个类显示到视图_Java_Android_Android Layout_Logcat - Fatal编程技术网

Java 试图从另一个类显示到视图

Java 试图从另一个类显示到视图,java,android,android-layout,logcat,Java,Android,Android Layout,Logcat,因此,当我试图在屏幕上显示一些文本时,我会得到一个错误,我将在下面发布。这个将要显示的文本假定实际显示一个变量。它将显示玩家获得的点数。我很久以前就从互联网上退出了。它可以编译,但是我得到了很多LogCat错误。这里是我试图显示文本的主要类 public class Main extends Activity { DrawingView v; @Override protected void onCreate(Bundle savedInstanceState)

因此,当我试图在屏幕上显示一些文本时,我会得到一个错误,我将在下面发布。这个将要显示的文本假定实际显示一个变量。它将显示玩家获得的点数。我很久以前就从互联网上退出了。它可以编译,但是我得到了很多LogCat错误。这里是我试图显示文本的主要类

    public class Main extends Activity {
    DrawingView v;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


        LinearLayout layout1 = new LinearLayout (this);
        FrameLayout game = new FrameLayout(this);
        DrawingView v = new DrawingView (this);

        TextView myText = new TextView(this);

        Button redCircle = new Button(this);



        redCircle.setWidth(300);
        redCircle.setText(DrawingView.addPoints);


        layout1.addView(myText);
        layout1.addView(redCircle); 
        redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        game.addView(myText);
        game.addView(v);
        game.addView(layout1);
        setContentView(v);
        //redCircle.setOnClickListener((OnClickListener) this);
    }
    public void onClick(View v) {
        Intent intent = new Intent(this, Main.class);
            startActivity(intent);

        // re-starts this activity from game-view. add this.finish(); to remove from stack
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}
这是我得到的LogCat错误。请不要否决这个问题,因为它描述得非常详细,语法错误很少。我知道你们所有的网虫,纳粹会尽你们最大的努力去做你们最擅长的事情

    04-20 01:51:42.998: E/AndroidRuntime(3154): FATAL EXCEPTION: main
04-20 01:51:42.998: E/AndroidRuntime(3154): Process: com.Tripps.thesimplegame, PID: 3154
04-20 01:51:42.998: E/AndroidRuntime(3154): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: android.content.res.Resources$NotFoundException: String resource ID #0x0
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.os.Looper.loop(Looper.java:135)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at java.lang.reflect.Method.invoke(Native Method)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at java.lang.reflect.Method.invoke(Method.java:372)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-20 01:51:42.998: E/AndroidRuntime(3154): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.content.res.Resources.getText(Resources.java:274)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.widget.TextView.setText(TextView.java:4122)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at com.Tripps.thesimplegame.Main.onCreate(Main.java:38)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.Activity.performCreate(Activity.java:5933)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-20 01:51:42.998: E/AndroidRuntime(3154):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-20 01:51:42.998: E/AndroidRuntime(3154):     ... 10 more
这是另一个类,可能会很有帮助,也可能会让你很困惑

public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;

     //TextView myTextView = new TextView(this);



     //redCircle.setWidth(300);
     //redCircle.setText("Start Game");


    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);

        }
    };

    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(updateCircle);
    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // your other stuff here
        if(random == null){
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }else {
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }

        canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
    }

    public boolean onTouch(View v, MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   if(isInsideCircle(x, y) ==  true){
      //Do your things here
       if(redColor == lastColor){
          Intent i = new Intent(v.getContext(), YouFailed.class);
          v.getContext().startActivity(i);
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

public boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius)))
    return true;
  return false;    
}











}
publicDrawingView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
RectF RectF=新的RectF(0,0,200,0);
专用静态最终int w=100;
公共静态int lastColor=Color.BLACK;
私有最终随机=新随机();
私人最终油漆=新油漆();
专用最终整数半径=230;
私有最终处理程序=新处理程序();
公共静态int redColor=Color.RED;
公共静态int greenColor=Color.GREEN;
int-randomWidth=0;
int-randomHeight=0;
公共静态int addPoints=0;
//TextView myTextView=新的TextView(此);
//红色圆圈。设置宽度(300);
//redCircle.setText(“开始游戏”);
private final Runnable updateCircle=new Runnable(){
@凌驾
公开募捐{
lastColor=random.nextInt(2)==1?红色:绿色;
paint.setColor(lastColor);
使无效();
handler.postDelayed(这个,1000);
}
};
@凌驾
受保护的无效数据附加到DOWINDOW(){
super.onAttachedToWindow();
handler.post(更新循环);
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//你的其他东西在这里
if(random==null){
randomWidth=(int)(random.nextInt(Math.abs(getWidth()-radius/2))+radius/2f);
randomHeight=(random.nextInt((int)Math.abs((getHeight()-radius/2+radius/2f));
}否则{
randomWidth=(int)(random.nextInt(Math.abs(getWidth()-radius/2))+radius/2f);
randomHeight=(random.nextInt((int)Math.abs((getHeight()-radius/2+radius/2f));
}
画布.画圈(随机宽度,随机高度+半径/2f,半径,绘制);
}
公共布尔onTouch(视图v,运动事件){
int x=(int)event.getX();
int y=(int)event.getY();
如果(isInsideCircle(x,y)=真){
//在这里做你的事情
if(redColor==lastColor){
Intent i=新Intent(v.getContext(),YouFailed.class);
v、 getContext().startActivity(i);
}否则{
addPoints++;
}
}否则{
}
返回true;
}
公共布尔IsInsidercle(整数x,整数y){
如果(((x-随机宽度)*(x-随机宽度))+((y-随机高度)*(y-随机高度))<((半径)*(半径)))
返回true;
返回false;
}
}
错误在

int w = getResources().getInteger(Color.RED);
getInteger()
需要一个
res
Color.RED
的值
0xffff0000
在res中找不到。因此出现错误

Color.RED
已经是一个整数。您的资源值也是整数。因此,当它试图查找值为
Color.RED
0xffff0000
的资源时,它给出了错误,因为不存在这样的资源

从代码看,似乎没有使用XMLs来设置布局。所以你使用的方式

int w = getResources().getInteger(Color.RED);
Button redCircle = (Button) findViewById(w);
似乎不正确。因为你的布局没有这样的对象。您应该创建一个新按钮

Button redCircle = new Button(context);
并将其添加到布局中

而且

setText()
需要字符串或
char[]
。如果
addPoints
不是字符串,您可能也应该更改它。您还可以使用
R.string.some\u string
传递一些
res
值,您应该在res/values文件夹中定义这些值。对于演示,将其更改为
redCicle.setText(“红色圆圈”)

DrawingView.addPoints
也是整数。因此,当您通过
redCircle.setText(DrawingView.addPoints)时res
值,因为它们是基于整数映射的。在这里,
addPoints
值似乎是0,因此,编译器查找映射值
0
未找到的资源,因此抛出错误
android.content.res.Resources$NotFoundException:String resource ID#0x0

TL;DR:
资源
值基于整数映射
setText()
findViewById()
getResources().getInteger()
,等等需要一个映射到特定资源的整数。如果您(错误地)传递了某个随机整数,它将查找该资源,因为编译器是哑的(可能是。可能不是),并且在找不到资源时会抛出异常

canvas.drawText("Score: " + addPoints, 120, 300, paint);

如果其他人有此问题,这是正确的方法

你能清理你的项目吗?@devin你把视图添加到“游戏”中,然后把contentView设置为“v”?我有点想法,但我会用什么来代替它?我不知道“res”值是什么?“res”基本上是你在资源中保留的东西。您的自定义颜色、布局、绘图等。您的android基础似乎有点薄弱。您应该快速完成基本操作;
canvas.drawText("Score: " + addPoints, 120, 300, paint);