有 Java 编程相关的问题?

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

java Dilog在安卓应用程序中不起作用

我有两个班SqlLiteExample.javaHotOrNot.java。第一个用于创建接口,第二个用于创建数据库

sqllite示例。java

package com.thenewboston;
import 安卓.app.Activity;
import 安卓.app.Dialog;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;

public class SqlLiteExample extends Activity implements OnClickListener{
EditText etName,etHotness;
Button btnSave,btnView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqliteexample);
        etName=(EditText) findViewById(R.id.editText1);
        etName=(EditText) findViewById(R.id.editText2);
        btnSave=(Button) findViewById(R.id.btnSQLUPDATE);
        btnView=(Button) findViewById(R.id.btnSQLVIEW);
        btnSave.setOnClickListener(this);
        btnView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){

        case R.id.btnSQLUPDATE:
            boolean didItWork=true;
            try{
        String name=etName.getText().toString();
        String hotness=etHotness.getText().toString();
        HotOrNot entry= new HotOrNot(SqlLiteExample.this);
        entry.open();
        entry.createEntry(name,hotness);
        entry.close();
            }catch(Exception e){
                didItWork= false;
            }finally{
                if(didItWork){
                    Dialog d= new Dialog(this);
                    d.setTitle("Heck yea");
                    System.out.println("testing");
                    TextView tv= new TextView(this);
                    tv.setText("sucess");
                    d.setContentView(tv);
                    d.show();
                }
            }


        break;
        case R.id.btnSQLVIEW:

        break;
        }

    }


}

霍托诺。java

package com.thenewboston;

import 安卓.content.ContentValues;
import 安卓.content.Context;
import 安卓.database.SQLException;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;

public class HotOrNot {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "persons_name";
    public static final String KEY_HOTNESS = "persons_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourhelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " TEXT NOT NULL, " + KEY_HOTNESS + "TEXT NOT NULL);"

            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST "+ DATABASE_TABLE);
            onCreate(db);

        }

    }

    public HotOrNot(Context c) {
        ourContext = c;
    }
    public HotOrNot open() throws SQLException{
        ourhelper = new DbHelper(ourContext);
        ourDatabase= ourhelper.getWritableDatabase();
        return this;
    }
    public void close(){
        ourhelper.close();
    }
    public long createEntry(String name, String hotness) {
        // TODO Auto-generated method stub
        ContentValues cv= new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }
}

问题在于每当我按下btnSave时,都应该显示一个dilog,它是在case R.id.btnSQLUPDATE:finally块中定义的,但它不是。我试图调试代码,在同一个case中执行HotOrNot entry= new HotOrNot(SqlLiteExample.this);后,它将移动到catch块并捕获异常。我能找到我做错的地方


共 (3) 个答案

  1. # 1 楼答案

    我认为您在这一行中得到了NPL(空指针验证):

    String hotness=etHotness.getText().toString();  
    

    您没有找到“ethodity”,请查看您的代码:
    onCreate()中,您可以这样写:

    etName=(EditText) findViewById(R.id.editText1);
    etName=(EditText) findViewById(R.id.editText2);
    

    应改为:

    etName=(EditText) findViewById(R.id.editText1);
    etHotness=(EditText) findViewById(R.id.editText2);
    
  2. # 2 楼答案

    我使用AlertDialog,它是更好的解决方案。使用此代码:

       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                LayoutInflater inflater = getActivity().getLayoutInflater();
    
                View viewDialog = inflater.inflate(R.layout.alertdialog_gameover, null);
                TextView title = (TextView)viewDialog.findViewById(R.id.title);
                title.setText("Successfully");
    
                builder.setView(viewDialog)
                        .setPositiveButton("Play again", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               //Set something for positive
    
                            }
                        })
                        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               //set something for negative
                            }
                        });
                //create dialog
                builder.create();
    //show dialog
                builder.show();
    

    编辑:

    尝试更改:

    }catch(Exception e){
                    didItWork= false;
                }
    

    致:

    }catch(Exception e){
                    didItWork= true;
                }
    

    你的对话代码对我有用

  3. # 3 楼答案

    我个人将AlertDialog与AlertDialog一起使用。这种情况下的建设者。效果很好

    new AlertDialog.Builder(this)
        .setTitle("Your title")
        .setMessage("Your message")
        .setPositiveButton("OK", mOnClickListener)
        .create()
        .show();
    

    您还可以使用setAdapter、setSingleChoiceItems和setMultiChoiceItems方法将可单击项目的列表设置为视图,或复选框或单选按钮的列表。我发现它使用起来更简单