Skip to content
Advertisement

My android studio app crashes when I touch the screen

My app keeps crashing on the mainActivity screen everytime I touch empty space on the screen. I have three buttons, two radioButtons, a textview, and two editText fields; whenever I touch these my app works fine, but if I ever touch white space, then the whole app crashes. I have minSDk of 15 and targetSDK of 23. I am adding Users to a database using SQLiteOpenHelper. Users have a name, password, and type (buyer or seller). Here is my code:

MainActivity.java

package com.example.cristiannavarrete.my_shopping;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//    private String userName;
//    private String userPass;
    MyDBHandler db;
    private Button logIn, addUser, clear;
    private EditText userField, passField;
    private RadioButton buyer;
    private RadioButton seller;
    //Singleton instance = Singleton.getInstance();

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

        db = new MyDBHandler(this, null, null, 4);

        logIn = (Button) findViewById(R.id.button);
        addUser = (Button) findViewById(R.id.button2);
        clear = (Button) findViewById(R.id.clear);
        userField = (EditText) findViewById(R.id.editText);
        passField = (EditText) findViewById(R.id.editText2);
        buyer = (RadioButton) findViewById(R.id.radioButton);
        seller = (RadioButton) findViewById(R.id.radioButton2);

        logIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (db.hasUser(userField.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "Log In successful", Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(getApplicationContext(), "Log In bad", Toast.LENGTH_SHORT).show();
            }
        });

        addUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User user = new User(userField.getText().toString(), passField.getText().toString(), "buyer");
                user.setId(db.addUser(user));
                Toast.makeText(getApplicationContext(), Integer.toString(user.getId()), Toast.LENGTH_SHORT).show();
            }
        });

        seller.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (seller.isChecked())
                    buyer.setChecked(false);
            }
        });

        buyer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buyer.isChecked())
                    seller.setChecked(false);
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.deleteAllRows();
            }
        });



    }


}

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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
        android:onClick="clear">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/LogIn"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="45dp"
            android:textStyle="bold"
            android:textColor="#5e00ff"
            android:textSize="25sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/UserName"
            android:id="@+id/textView2"
            android:layout_marginTop="42dp"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Pass"
            android:id="@+id/textView3"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="60dp"
            android:textStyle="bold" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/LogInButton"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="29dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/AddUser"
            android:id="@+id/button2"
            android:layout_alignTop="@+id/button"
            android:layout_toRightOf="@+id/textView"
            android:layout_toEndOf="@+id/textView" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/NewBuyer"
            android:id="@+id/radioButton"
            android:textSize="15sp"
            android:layout_below="@+id/button2"
            android:layout_centerHorizontal="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/NewSeller"
            android:id="@+id/radioButton2"
            android:layout_below="@+id/radioButton"
            android:layout_alignLeft="@+id/radioButton"
            android:layout_alignStart="@+id/radioButton"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/clearDatabase"
            android:id="@+id/clear"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cristiannavarrete.my_shopping" >

    <application
        android:name=".global"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SellerMainPage"
            android:label="Seller Main Page">
        </activity>
        <activity
            android:name=".BuyerMainPage"
            android:label="Buyer Main Page">
        </activity>
        <activity
            android:name=".ItemInfoPage"
            android:label="Item Info Page">
        </activity>
    </application>

</manifest>

Can anyone tell me why this happens. Thanks in advance for the help!

Advertisement

Answer

Using in your layout the attribute

`android:onClick="clear" 

you have to implement inside your Activity a method with the same name

public void clear(View v) {
    // do something
}

The View passed into the method is a reference to the widget that was clicked.

When a user clicks the view, the Android system calls the activity’s clear(View) method.

Pay attention: if you are using a fragment, Android will not look for the onClick method in the fragment but only in the current Activity.

You can achieve the same using:

View myClickableView = findViewById(R.id.myView);
myClickableView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        clear(v);
    }
});

public void clear(View v) {
        // do something
}
Advertisement