java Dilog在安卓应用程序中不起作用
我有两个班SqlLiteExample.java
和HotOrNot.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块并捕获异常。我能找到我做错的地方
# 1 楼答案
我认为您在这一行中得到了NPL(空指针验证):
您没有找到“ethodity”,请查看您的代码:
在
onCreate()
中,您可以这样写:应改为:
# 2 楼答案
我使用AlertDialog,它是更好的解决方案。使用此代码:
编辑:
尝试更改:
致:
你的对话代码对我有用
# 3 楼答案
我个人将AlertDialog与AlertDialog一起使用。这种情况下的建设者。效果很好
您还可以使用setAdapter、setSingleChoiceItems和setMultiChoiceItems方法将可单击项目的列表设置为视图,或复选框或单选按钮的列表。我发现它使用起来更简单