1

我正在處理一個小型項目,其中需要將電話號碼及其相應的地址存儲在Android的sqlite數據庫中。我的MainActivity有一個佈局,其中用戶有兩個選項:1.他可以向數據庫添加新值(電話號碼和地址)2.他可以通過輸入電話號碼搜索地址。無法啓動Android中的活動組件信息和Java空指針異常

這是我的MainActivity:

public class MainActivity extends Activity { 


    private static final String TABLE_CONTACTS = "contacts"; 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "contactsManager"; 
    // Contacts Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_ADDRESS = "name"; 
    private static final String KEY_PH_NO = "phone_number"; 

    SQLiteDatabase db; 

    TextView numberTextview,addressTextView,fetchedAddressTextView; 
    Button buttonSave,buttonSearch; 
    EditText numberEditText,addressEditText,searchEditText; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     numberTextview=(TextView)findViewById(R.id.textView1); 
     numberEditText=(EditText)findViewById(R.id.editText1); 
     addressTextView=(TextView)findViewById(R.id.textView2); 
     addressEditText=(EditText)findViewById(R.id.editText2); 
     buttonSave=(Button)findViewById(R.id.button1); 
     searchEditText=(EditText)findViewById(R.id.editText3); 
     buttonSearch=(Button)findViewById(R.id.button2); 
     fetchedAddressTextView=(TextView)findViewById(R.id.textView3); 

     try { 
      String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTACTS + "(" 
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ADDRESS + " TEXT," 
        + KEY_PH_NO + " TEXT" + ")"; 
      db.execSQL(CREATE_CONTACTS_TABLE); 
      Toast.makeText(MainActivity.this, "table created ", Toast.LENGTH_LONG).show(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     buttonSave.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       String address=addressEditText.getText().toString(); 
       long number = Integer.parseInt(numberEditText.getText().toString()); 

       try { 
        ContentValues values = new ContentValues(); 
        values.put(KEY_ADDRESS, address); // Contact Address 
        values.put(KEY_PH_NO, number); // Contact Phone 

        // Inserting Row 
        db.insert(TABLE_CONTACTS, null, values); 
        db.close(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      } 
     }); 

     buttonSearch.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       long phone_number=Integer.parseInt(searchEditText.getText().toString()); 

       try { 
        Cursor c=db.query(TABLE_CONTACTS, new String[]{ 
          KEY_ADDRESS 
        }, 
        KEY_PH_NO + "=" + phone_number, 
        null, 
        null, 
        null, 
        null); 

        if (c != null) { 
         c.moveToFirst(); 
        } 
        fetchedAddressTextView.setText(c.toString()); 
       } catch (Exception e) { 

        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 




     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

的logcat的痕跡:

03-28 11:45:12.562: E/AndroidRuntime(4079): FATAL EXCEPTION: main 
03-28 11:45:12.562: E/AndroidRuntime(4079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fivetofive/com.example.fivetofive.MainActivity}: java.lang.NullPointerException 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread.access$600(ActivityThread.java:140) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.os.Looper.loop(Looper.java:137) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread.main(ActivityThread.java:4895) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at dalvik.system.NativeStart.main(Native Method) 
03-28 11:45:12.562: E/AndroidRuntime(4079): Caused by: java.lang.NullPointerException 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at com.example.fivetofive.MainActivity.onCreate(MainActivity.java:59) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.Activity.performCreate(Activity.java:5163) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061) 
03-28 11:45:12.562: E/AndroidRuntime(4079):  ... 11 more 

應用程序並不甚至開始。它運行時崩潰。這是我第一次在android中使用sqlite。所以請原諒任何愚蠢的錯誤,並指出它們。

在此先感謝。

UPDATE 請注意:我已經去爲延伸SQLiteOpenHelper單獨DataBaseHandler類解決我的問題現在。我仍然不是爲什麼我得到了我雖然到達這裏的錯誤。現在沒有時間,所以我會在稍後回來,如果有的話我會得到答案。對於那些會遇到這個問題的人,我只想說,製作DataBaseHandler課程有很大幫​​助。

+0

在這行你收到此錯誤去了?你的59號線是哪一個? – Nirali 2013-03-28 06:30:32

+0

@Nirali:db。execSQL(CREATE_CONTACTS_TABLE);這是我行號59 – 2013-03-28 06:32:28

+2

的代碼hi @abhi_is_learning_android:請向我們展示您的數據庫幫助類。 – 2013-03-28 06:45:45

回答

0

請檢查您從此獲得價值嗎?

String address=addressEditText.getText().toString(); 
long number = Integer.parseInt(numberEditText.getText().toString()); 

如果是,那麼改變這行:

ContentValues values = new ContentValues(); 
        values.put(KEY_ADDRESS, address); // Contact Address 
        values.put(KEY_PH_NO, number); // Contact Phone 

這個

db.open(); 
ContentValues values = new ContentValues(); 
values.put(KEY_ADDRESS, ""+address); // Contact Address 
values.put(KEY_PH_NO, ""+number); // Contact Phone 

因爲在查詢KEY_PH_NOTEXT

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTACTS + "(" 
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ADDRESS + " TEXT," 
        + KEY_PH_NO + " TEXT" + ")"; 
+0

Hi @JDroid。我已經在我的MainActivity本身中聲明瞭所有必需的東西。我沒有sqlitehelper類,所以我沒有聲明SqliteDatabase構造函數anywher。有輔助課是絕對必要的嗎?如果不是,那麼你認爲哪裏錯了?我嘗試了您提出的更改,錯誤仍然保持不變。 – 2013-03-28 06:54:23

1

你應該打開你的數據庫是在使用它之前..只需將下面的兩行放在try {}然後嘗試讓我知道發生了什麼。

db = new SQLiteDatabase(); // pass context or activity its upon your SQLiteDatabase() class 
db.open(); 

我認爲它可以幫助你..

2

而且你必須extends SQLiteOpenHelper {

請初始化您分貝低於線

db = new SQLiteDatabase(); 

對象,並有你加入這行清單

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

請通過本教程

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

http://www.vogella.com/articles/AndroidSQLite/article.html

+0

當我將該行添加到代碼時,出現錯誤: '構造函數SQLiteDatabase()不可見' – 2013-03-28 06:38:57

+0

您將通過我在答案中提到的教程。它會幫助您 – Nirali 2013-03-28 06:40:26

+1

hello @abhi_is_learning_android:在您的SQLiteDatabase類(sqlite助手類)中將構造函數設置爲public。 – 2013-03-28 06:42:20

相關問題