【问题标题】:Canvas added to activity_main.xml crashes my app添加到 activity_main.xml 的画布使我的应用程序崩溃
【发布时间】:2018-02-07 08:49:17
【问题描述】:

我的问题:

我一直在尝试制作一个应用程序,让球根据方向变化弹跳。我遇到了一些问题,所以我以不同的方式开始了这个项目。当我使用箭头键时,球向上/向下/向右/向左移动。球本身移动得很好,屏幕上可以看到文字(至少我曾经看到过!)但我无法将这两个放在屏幕上。

每次我将 BouncingBall 添加到 activity_main.xml 时,应用程序都会开始崩溃,并且屏幕上什么也看不到。 当我从 activity_main.xml 中删除 BouncingBall 时,我会再次看到球并且还可以移动它但看不到文字。似乎是什么官员,问题?我的大脑不再正常工作,代码中可能有一些我看不到的有趣的东西。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.maija.pomppupallo.MainActivity">

<com.example.maija.pomppupallo.BouncingBallView
    android:id="@+id/bouncingBallView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/hellou"
    android:layout_width="195dp"
    android:layout_height="70dp"
    android:text="@string/hello"
    android:textColor="@color/colorAccent"
    android:textSize="30sp"
    android:translationZ="100dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textorientation"
    android:layout_width="287dp"
    android:layout_height="50dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="64dp"
    android:text="@string/orientation"
    android:textColor="@android:color/holo_red_dark"
    android:textSize="30sp"
    android:translationZ="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.506"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

主Activity.java

package com.example.maija.pomppupallo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView tv1, tv2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate (savedInstanceState);
    setContentView(R.layout.activity_main);

    View BouncingBallView = new BouncingBallView(this);
    setContentView (BouncingBallView);

}
}

BouncingBallView.java

package com.example.maija.pomppupallo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;

public class BouncingBallView extends View {

private int xMin = 0;
private int xMax;
private int yMin = 0;
private int yMax;
private float ballRadius = 130;
private float ballX = ballRadius + 20;
private float ballY = ballRadius + 40;
private float ballSpeedX = 0;
private float ballSpeedY = 0;
private RectF ballBounds;
private Paint paint;

public BouncingBallView(Context context) {
    super (context);
    ballBounds = new RectF();
    paint = new Paint();

    this.setFocusable(true);
    this.requestFocus();
}



@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

    switch (keyCode){
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        ballSpeedY=0;
            if (ballSpeedX > 0) {
                ;
            }
            else ballSpeedX = 20;
            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
        ballSpeedY=0;
            if (ballSpeedX < 0) {
                ;
            }
            else ballSpeedX = -20;
            break;

        case KeyEvent.KEYCODE_DPAD_UP:
        ballSpeedX=0;
            if (ballSpeedY < 0) {
                ;
            }
            else ballSpeedY = -20;
        break;

        case KeyEvent.KEYCODE_DPAD_DOWN:
        ballSpeedX=0;
            if (ballSpeedY > 0) {
                ;
            }
            else ballSpeedY = 20;
        break;

        case KeyEvent.KEYCODE_DPAD_CENTER:
            ballSpeedX=0;
            ballSpeedY=0;
            break;

        case KeyEvent.KEYCODE_A:
            float maxRadius = (xMax>yMax) ? yMax/2*0.9f : xMax/2*0.9f;
            if (ballRadius < maxRadius) {ballRadius*=1.05;}
            break;

        case KeyEvent.KEYCODE_Z:
            if (ballRadius > 20){ballRadius*=0.95;}
            break;
    }
    return true;
}



@Override
protected void onDraw(Canvas canvas) {
    ballBounds.set (ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
    paint.setColor (Color.CYAN);
    canvas.drawOval (ballBounds, paint);

    update();

    try {
        Thread.sleep (30);
    }catch (InterruptedException e){}
    invalidate ();
}

private void update(){
    ballX+=ballSpeedX;
    ballY+=ballSpeedY;

    if(ballX+ballRadius > xMax){
        ballSpeedX = -ballSpeedX;
        ballX = xMax-ballRadius;
    }else if (ballX-ballRadius < xMin){
        ballSpeedX = -ballSpeedX;
        ballX = xMin+ballRadius;
    }

    if(ballY+ballRadius > yMax){
        ballSpeedY = -ballSpeedY;
        ballY = yMax-ballRadius;
    }else if (ballY-ballRadius < yMin){
        ballSpeedY = -ballSpeedY;
        ballY = yMin+ballRadius;
    }
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    xMax = w-1;
    yMax = h-1;
}
}

AndroidOrientationSensor.java

package com.example.maija.pomppupallo;


import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidOrientationSensor extends Activity{

TextView textviewOrientation, tv1, tv2;
OrientationEventListener myOrientationEventListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textviewOrientation = (TextView)findViewById(R.id.textorientation);
    tv1 = (TextView) findViewById (R.id.textorientation);
    tv2 = (TextView) findViewById (R.id.hellou);

    myOrientationEventListener
            = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

        @Override
        public void onOrientationChanged(int arg0) {
            // TODO Auto-generated method stub
            textviewOrientation.setText("Orientation is: " + String.valueOf(arg0));
        }};

    if (myOrientationEventListener.canDetectOrientation()){
        Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
        myOrientationEventListener.enable();
    }
    else{
        Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
        finish();
    }





}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    myOrientationEventListener.disable();
}
}

我希望有人能理解这里的问题。

注意:传感器文件尚未准备好,存在一些错误。

【问题讨论】:

    标签: android android-activity android-canvas android-view textview


    【解决方案1】:
    Remove below lines from your code as you already add BouncingBallView in xml.
    
     View BouncingBallView = new BouncingBallView(this);
        setContentView (BouncingBallView);
    

    【讨论】:

    • 感谢您的回答!所以这就是堵塞整个应用程序的方式。现在,当我从 xml 和 MainActivity 中删除 BouncingBallViews 时,我看到了文本。当我按此顺序View BouncingBallView = new BouncingBallView(this); setContentView (BouncingBallView); setContentView(R.layout.activity_main); 时,我看到的是文字,而不是球。当我在 MainActivity 中删除这两行并在 xml 中添加球时,应用程序崩溃了。如何让文字和球同时出现?
    • 只需删除 View BouncingBallView = new BouncingBallView(this); setContentView (BouncingBallView) 并运行应用程序,一切正常。不要从 xml 中删除视图。
    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多