有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java代码并没有错误,logcat也并没有任何错误,但不幸的是应用程序停止了错误

package com.example.darshreddy.diser;

import 安卓.annotation.SuppressLint;
import 安卓.app.ActionBar;
import 安卓.app.Activity;
import 安卓.app.Dialog;
import 安卓.content.Context;
import 安卓.database.sqlite.SQLiteOpenHelper;
import 安卓.os.Bundle;
import 安卓.os.PersistableBundle;
import 安卓.support.annotation.Nullable;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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;}
    }
}

这是我为执行SQLite数据库相关文件而编写的代码。我有 将注册从链接到它,错误为“不幸的是,应用程序已停止”。如果有错误,请帮我找到。或者建议我做些改变。 logcat显示

"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 安卓.view.View$OnClickListener"


共 (1) 个答案

  1. # 1 楼答案

    您需要实现OnClickListener接口,如:

    public class SQLite extends Activity implements View.OnClickListener {
    

    另外,按照@Brett Jeffreson的建议,在catch block中添加e.printStackTrace(),以获取日志中的错误/崩溃跟踪