应用程序无法打开 - 没有错误

时间:2016-11-02 17:48:00

标签: android android-studio

我的应用无法打开。我已经尝试了很多东西来完成这项工作,但没有任何结果。我的应用程序应该这样做:

  • 当我点击一个按钮时,它可以工作,当我从这个按钮拖到第二个按钮时,首先停止工作,然后第二个按钮启动。

任何帮助将不胜感激。

MainActivity.java

package com.example.android.test;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View; 
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.test.R;

public class MainActivity extends AppCompatActivity {

final TextView viewA = (TextView) findViewById(R.id.textView);
final TextView viewB = (TextView) findViewById(R.id.textView2);
Rect outRect = new Rect();
int[] location = new int[2];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewB.setOnTouchListener(new View.OnTouchListener() {


        public boolean onTouch(View v, MotionEvent event) {
            int x = (int) event.getRawX();
            int y = (int) event.getRawY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (isViewInBounds(viewA, x, y))
                    viewA.dispatchTouchEvent(event);
                else if (isViewInBounds(viewB, x, y)) {

                viewB.setText("DONE");
                }
            }

            return false;
        }
    });

    viewB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        }
    });

    viewA.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        }
    });

    viewB.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            int x = (int) event.getRawX();
            int y = (int) event.getRawY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (isViewInBounds(viewB, x, y))
                    viewB.dispatchTouchEvent(event);
                else if (isViewInBounds(viewA, x, y)) {

                   viewA.setText("DONE");
                }
            }

            return false;
        }
    });


}


private boolean isViewInBounds(View view, int x, int y) {
    view.getDrawingRect(outRect);
    view.getLocationOnScreen(location);
    outRect.offset(location[0], location[1]);
    return outRect.contains(x, y);
  }
}

activity_main.xml中

?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_margin="90dp"
tools:context="com.example.android.test.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@android:color/holo_green_light"
    android:padding="40dp"
    android:text="FIRST"
    android:textColor="@android:color/black"
    android:textSize="30sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/holo_red_dark"
    android:padding="40dp"
    android:text="SECOND"
    android:textColor="@android:color/black"
    android:textSize="30sp" />

1 个答案:

答案 0 :(得分:1)

您的应用无法打开,因为它崩溃了。您正尝试在setContentView()之前初始化您的观看次数:

public class MainActivity extends AppCompatActivity {

final TextView viewA = (TextView) findViewById(R.id.textView);
final TextView viewB = (TextView) findViewById(R.id.textView2);

您必须将它们设为全局并在setContentView()后初始化它们,并且您不需要将它们设为最终版,请使用私有:

public class MainActivity extends AppCompatActivity {

private TextView viewA;
private TextView viewB;
private Rect outRect = new Rect();
private int[] location = new int[2];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewA = (TextView) findViewById(R.id.textView);
    viewB = (TextView) findViewById(R.id.textView2);

之后,您可以使用它们。并且“没有错误”......事实并非如此。你肯定会以你的方式得到一个例外。您必须调整logcat过滤器。