Old York
Old York

Reputation: 73

App doesn't open - no errors

My app does not open. I've tried many things to make this work but without any results. My app is supposed to do this:

Any help will be appreciated.

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" />

Upvotes: 0

Views: 73

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

Your app does not open because it crashs. You are trying to initialize your views before setContentView():

public class MainActivity extends AppCompatActivity {

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

You have to make them global and initialize them after setContentView(), and you don´t need to make them final, use private:

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);

after that, you can use them. And "No errors"....that isn´t the case. You definitely getting an exception in the way you have done it. You have to adjust your logcat filters.

Upvotes: 1

Related Questions