所以我试图制作一个图书馆助理应用程序和我想要做什么,每当我点击一个按钮时,一个对话框必须打开,在那个对话框中我必须放一个书名,它必须保存在数据库上实现了所有的东西但是当点击按钮时会打开一个对话框,当我输入书的名字并按下确定时,我在下面弹出的错误弹出
错误
FATAL EXCEPTION: main Process: com.example.vishal.thadarinew, PID: 28712
Attempt to invoke virtual method 'boolean com.example.vishal.thadarinew.Database.insertData(java.lang.String)' on a null object reference
at com.example.vishal.thadarinew.MainActivity$1$2.onClick(MainActivity.java:103)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
MainActivty.java
public class MainActivity extends AppCompatActivity {
private Button issueButton;
private Button reissueButton;
private Button returnButton;
Database myDb;
private EditText editQuantity;
public AlertDialogFragment malert;
String book;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
issueButton = (Button) findViewById(R.id.issueid);
reissueButton = (Button) findViewById(R.id.reissueid);
returnButton = (Button) findViewById(R.id.returnid);
issueButton();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
alertuserAboutError();
return true;
}
if (id == R.id.action_about) {
Intent intent = new Intent(this, About.class);
startActivity(intent);
}
return true;
}
public void alertuserAboutError() {
final AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "Dialog");
}
public void issueButton(){
issueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editQuantity = new EditText(MainActivity.this);
editQuantity.setInputType(InputType.TYPE_CLASS_TEXT);
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.app_name)
.setMessage("Please Enter TIME")
.setNegativeButton("CANCEL", null)
.setView(editQuantity)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean isInserted = myDb.insertData(editQuantity.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});}}
Database.java
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME="Assistaant.db";
public static final String TABLE_NAME="Books";
public static final String COL_2="BNAME";
public static final String COL_3="DOI";
String pattern="dd-MM-yyyy";
Context con;
public Database(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (BNAME TEXT,DOI TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public String today(){
String date=new SimpleDateFormat(pattern).format(new Date());
return date;
}
public boolean insertData(String bname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,bname);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public void putreturn(int date){
String strdate=date+"";
try{
FileOutputStream fileout=con.openFileOutput("date.txt", Context.MODE_PRIVATE);
fileout.write(strdate.getBytes());
fileout.close();
}
catch(FileNotFoundException f){
Toast.makeText(con,"Please try again",Toast.LENGTH_SHORT).show();
}
catch(IOException e){
Toast.makeText(con,"Please try again", Toast.LENGTH_SHORT).show();
}
}}