我有两个类SqlLiteExample.java
和HotOrNot.java
。第一个用于创建接口,第二个用于创建数据库。
SqlLiteExample.java
package com.thenewboston;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.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;
}
}
}
HotOrNot.java
package com.thenewboston;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.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
时出现的问题都应显示在finally
case R.id.btnSQLUPDATE:
HotOrNot entry= new HotOrNot(SqlLiteExample.this);
块中定义的稀释,但事实并非如此。我在执行后尝试调试代码case
在同一个function createHtml(array, index, elems) {
elems[index].innerHTML = array[index++];
if ((index + 1) <= array.length) {
setTimeout(function() {
createHtml(array, index, elems);
}, 200);
}
}
var newStringArr = cities[i].split('');
var tickerSpans = ticker.children;
createHtml(newStringArr, 0, tickerSpans);
中它正在进入catch区域并捕获异常。我可以找到我做错的地方。
答案 0 :(得分:1)
我认为你在这一行得到了NPL(空指针执行):
String hotness=etHotness.getText().toString();
你没有找到ViewById&#34; etHotness&#34; ,看看你的代码:
在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);
答案 1 :(得分:0)
我使用AlertDialog,这是更好的解决方案。使用此代码:
#content {
float: left; position: relative;
}
.imgupl {
position:absolute;
z-index: 1;
}
.imggrid {
position:absolute;
z-index: 3;
}
修改强>
尝试改变:
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;
}
您的Dialog代码适用于我。
答案 2 :(得分:0)
我个人使用AlertDialog和AlertDialog.Builder来处理这种情况。效果很好......
new AlertDialog.Builder(this)
.setTitle("Your title")
.setMessage("Your message")
.setPositiveButton("OK", mOnClickListener)
.create()
.show();
您还可以将可点击项列表设置为视图,或使用方法setAdapter,setSingleChoiceItems和setMultiChoiceItems设置复选框或单选按钮列表。我觉得使用它更简单。