Darsh Reddy
Darsh Reddy

Reputation: 49

Code has no error and logcat has nothing, but still unfortunately app stopped error

package com.example.darshreddy.diser;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class SQLite extends Activity {
    Button but;
    EditText nam, mail, pass;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        nam = (EditText) findViewById(R.id.name);
        pass = (EditText) findViewById(R.id.passw);
        mail = (EditText) findViewById(R.id.emi);
        but = (Button) findViewById(R.id.button2);
        but.setOnClickListener((View.OnClickListener) this);
    }
        public void onClick(View arg0){
        switch (arg0.getId()) {
            case R.id.button2:
                boolean didItwork=true;
                try {
                    String name = nam.getText().toString();
                    String passw = pass.getText().toString();
                    String eml = mail.getText().toString();

                    SQL entry = new SQL(SQLite.this);
                    entry.open();
                    entry.createEntry(name, passw, eml);
                    entry.close();
                } catch (Exception e) {
                    didItwork=false;
                }finally {
                    if (didItwork) {
                        Dialog d = new Dialog(this);
                        d.setTitle("Heck yea!!");
                        TextView tv = new TextView(this);
                        tv.setText("success");
                        d.setContentView(tv);
                        d.show();
                    }

                }
        break;}
    }
}

This is the code I wrote for executing SQLite database related file. I had linked a registration from with it and the error is "unfortunately the app has stopped". please help me find the error if any. or suggest me some changes. logcat shows

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.darshreddy.diser/com.example.darshreddy.diser.SQLite}: java.lang.ClassCastException: com.example.darshreddy.diser.SQLite cannot be cast to android.view.View$OnClickListener"

Upvotes: 0

Views: 63

Answers (1)

Firoz Memon
Firoz Memon

Reputation: 4680

You need to implement OnClickListener interface like:

public class SQLite extends Activity implements View.OnClickListener {

Also, add e.printStackTrace() in catch block as suggested by @Brett Jeffreson to get error/crash trace in log

Upvotes: 1

Related Questions