我正在尝试在表单中键入一些信息并将其保存到数据库中。但是,当我按下我的应用程序中的“保存”按钮时,它会给我以下错误。
我的logcat
07-21 13:41:08.162: E/AndroidRuntime(6190): FATAL EXCEPTION: main
07-21 13:41:08.162: E/AndroidRuntime(6190): java.lang.NullPointerException
07-21 13:41:08.162: E/AndroidRuntime(6190): at sp.com.DetailForm$1.onClick(DetailForm.java:110)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.view.View.performClick(View.java:4243)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.view.View$PerformClick.run(View.java:17520)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.os.Handler.handleCallback(Handler.java:725)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.os.Handler.dispatchMessage(Handler.java:92)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.os.Looper.loop(Looper.java:153)
07-21 13:41:08.162: E/AndroidRuntime(6190): at android.app.ActivityThread.main(ActivityThread.java:5299)
07-21 13:41:08.162: E/AndroidRuntime(6190): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 13:41:08.162: E/AndroidRuntime(6190): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 13:41:08.162: E/AndroidRuntime(6190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-21 13:41:08.162: E/AndroidRuntime(6190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-21 13:41:08.162: E/AndroidRuntime(6190): at dalvik.system.NativeStart.main(Native Method)
我的DetailForm
package sp.com;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.widget.TabHost;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.media.MediaPlayer;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class DetailForm extends Activity {
private EditText name = null;
private EditText author = null;
private EditText url = null;
private EditText duedate = null;
private RadioGroup category = null;
private BookHelper helper = null;
private Button save = null;
private MediaPlayer mp;
CheckBox chkbox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_form);
helper = new BookHelper(this);
name = (EditText) findViewById(R.id.name);
author = (EditText) findViewById(R.id.author1);
url = (EditText) findViewById(R.id.url);
category = (RadioGroup) findViewById(R.id.category1);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
mp = MediaPlayer.create(getBaseContext(), R.raw.sound);
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
boolean isChecked = sharedPref.getBoolean("checked", false);
if (isChecked == true) {
if (mp.isPlaying() == false) {
mp.start();
}
} else {
if (mp.isPlaying() == true) {
mp.stop();
}
}
}
@Override
public void onDestroy(){
super.onDestroy();
helper.close();
}
@Override
protected void onStop() {
super.onStop();
if (mp.isPlaying() == true){
mp.stop();
}
}
private View.OnClickListener onSave = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nameStr = name.getText().toString();
String authStr = author.getText().toString();
String urlStr = url.getText().toString();
String dueStr = duedate.getText().toString();
String catStr = "";
switch(category.getCheckedRadioButtonId()){
case R.id.action:
catStr = "Action";
break;
case R.id.mystery:
catStr = "Mystery";
break;
case R.id.thriller:
catStr = "Thriller";
break;
case R.id.romance:
catStr = "Romance";
break;
case R.id.horror:
catStr = "Horror";
break;
case R.id.others:
catStr = "Others";
break;
}
helper.insert(nameStr,authStr,dueStr,urlStr,catStr);
finish();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Intent intent = new Intent(DetailForm.this, Settings.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
我的主要活动
package sp.com;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.widget.TabHost;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.media.MediaPlayer;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class MainActivity extends ListActivity {
private Cursor model = null;
private BookAdapter adapter = null;
private BookHelper helper = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new BookHelper(this);
model=helper.getAll();
startManagingCursor(model);
adapter = new BookAdapter(model);
setListAdapter(adapter);
}
@Override
public void onDestroy(){
super.onDestroy();
helper.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.option,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
//TODO Auto-generated method stub
if(item.getItemId()==R.id.add){
startActivity(new Intent(MainActivity.this,DetailForm.class));
}
return super.onOptionsItemSelected(item);
}
class BookAdapter extends CursorAdapter{
BookAdapter(Cursor c){
super(MainActivity.this,c);
}
@Override
public void bindView(View row,Context ctxt,Cursor c){
BookHolder holder = (BookHolder) row.getTag();
holder.populateFrom(c,helper);
}
@Override
public View newView(Context ctxt,Cursor c,ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
BookHolder holder = new BookHolder(row);
row.setTag(holder);
return(row);
}
}
static class BookHolder {
private TextView bookName = null;
private TextView dueDate = null;
BookHolder(View row){
bookName = (TextView) row.findViewById(R.id.bookname);
dueDate = (TextView) row.findViewById(R.id.duedate);
}
void populateFrom(Cursor c, BookHelper helper){
bookName.setText(helper.getName(c));
dueDate.setText(helper.getDue(c));
}
}
}
最后,我的SQLiteOpenHelper
package sp.com;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class BookHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "books.db";
private static final int SCHEMA_VERSION = 1;
public BookHelper(Context context){
super(context, DATABASE_NAME,null,SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE books_table (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "name TEXT, author TEXT, url TEXT , duedate TEXT , category TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor getAll(){
return(getReadableDatabase().rawQuery(
"SELECT _id , name , author , url , duedate , category FROM books_table ORDER BY name", null));
}
public void insert(String name , String author , String url , String duedate , String category){
ContentValues cv = new ContentValues();
cv.put("name",name);
cv.put("author", author);
cv.put("url", url);
cv.put("duedate",duedate);
cv.put("category",category);
getWritableDatabase().insert("books_table", "name", cv);
}
public String getName(Cursor c){
return (c.getString(1));
}
public String getAuthor(Cursor c){
return (c.getString(2));
}
public String getUrl(Cursor c){
return (c.getString(3));
}
public String getDue(Cursor c){
return (c.getString(4));
}
public String getcat(Cursor c){
return (c.getString(5));
}
}
如果您需要有关我的代码的其他信息,请告诉我!谢谢。
此致 史蒂夫
答案 0 :(得分:1)
使用duedate
课程findViewById()
内的onCreate()
初始化Activity
。
答案 1 :(得分:0)
使用findViewById(R.id.duedate)初始化oncreate方法中的duedate;