2011-08-20 24 views
0

我有一個活動SaveData.class與公共方法的addEvent()的使用添加一些信息存儲在數據庫表如下調用的公共方法:的NullPointerException上從另一個活動

公共類保存數據擴展活動實現OnClickListener {

public SoftCopyDatabase dB ; 
public static String FILE_NAME; 
String _subject, _topic,_lecturenumber,_date; 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.save); 
    View add = findViewById(R.id.saveSave); 
    add.setOnClickListener(this); 
    View home = findViewById(R.id.saveBack); 
    home.setOnClickListener(this); 

}public void onStart() { 
    super.onStart(); 
    dB = new SoftCopyDatabase(this); 
} 

public void onStop() { 
    super.onStop(); 
    if (dB.getReadableDatabase().isOpen()) { 
     //dB.close(); 
    } 
} 
public void onDestroy() { 
    super.onDestroy(); 
    if (dB.getReadableDatabase().isOpen()) { 
     dB.close(); 
    } 
} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.saveBack: 
     Intent i = new Intent(this, OpenScreen.class); 
     startActivity(i); 
     break; 
    case R.id.saveSave: 

     EditText subject = (EditText) findViewById(R.id.subjectid); 
     EditText topic = (EditText) findViewById(R.id.topicid); 
     EditText lecturenumber = (EditText) findViewById(R.id.lecturenumberid); 
     EditText date = (EditText) findViewById(R.id.dateid); 
     _subject = ((TextView) subject).getText().toString(); 
     _topic = ((TextView) topic).getText().toString(); 
     _lecturenumber = ((TextView) lecturenumber).getText() 
       .toString(); 
     _date = ((TextView) date).getText().toString(); 
     FILE_NAME = _subject + _topic + _lecturenumber; 
     //addEvent(); 

     Intent j = new Intent(this, LectureNoting.class); 
     startActivity(j); 

     break; 

    } 
} 

public void addEvent() { 


    ContentValues values = new ContentValues(); 
    values.put(SUBJECT, _subject); 
    values.put(TOPIC, _topic); 
    values.put(LECTURENUMBER, _lecturenumber); 
    values.put(DATE, _date); 
    values.put(_DATA, FILE_NAME + ".png"); 
    dB.getWritableDatabase().insertOrThrow(TABLE_NAME, null, values); 
} 

}

另一個活動LectureNoting.class用於保存繪圖在磁盤上並更新在數據庫表中的條目,如下所示:

公共類LectureNoting延伸活動實現View.OnTouchListener {

private SaveData sD=new SaveData(); 
private File directory = new File("/sdcard/SoftCopy"); 
//...remaining code 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.drawing_activity); 
    } 
//...remaining code 
public void onClick(View view){ 
    switch (view.getId()){ 
case R.id.saveBtn: 
addEvent(); 
      final Activity currentActivity = this; 
    Handler saveHandler = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 

      Toast.makeText(currentActivity, "Lecture Saved", Toast.LENGTH_SHORT).show(); 

     } 
    } ; 
    new ExportBitmapToFile(this,saveHandler, softCopyInterface.getBitmap()).execute(); 
     break; 
//...remaining code 
} 
private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> { 
    private Context mContext; 
    private Handler mHandler; 
    private Bitmap nBitmap; 

    public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) { 
     mContext = context; 
     nBitmap = bitmap; 
     mHandler = handler; 
    } 

    @Override 
    protected Boolean doInBackground(Intent... arg0) { 

     try { 
      if (!directory.exists()) { 
       directory.mkdirs(); 
      } 

      final FileOutputStream out = new FileOutputStream(new File(directory + "/"+SaveData.FILE_NAME+".png")); 

      nBitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
      out.flush(); 
      out.close(); 

      return true; 
     } 

     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return false; 
    } 


    @Override 
    protected void onPostExecute(Boolean bool) { 
     super.onPostExecute(bool); 
     if (bool){ 
      mHandler.sendEmptyMessage(1); 
     } 
    } 
} 
} 

而我收到以下錯誤:

 Unable to start activity componentInfo(com.ned.LectureNoting):NullPointerException 

在的addEvent(),在LectureNoting的onclick方法中使用。

請告訴我我哪裏出錯了。我想提到的一點是,如果addEvent()是從定義它的同一活動中調用的,則不會顯示此錯誤。

+0

查看logcat,在debug下查看錯誤實際發生的位置,然後可以在Log.i語句中查看可能爲空的內容。另外,第二項活動是有人再次按下按鈕以保存消息? –

回答

1

幾件事情:

  1. 的logcat應該給予更多的錯誤信息。您可能需要向下滾動一下以查看代碼中問題的來源,但應該有更多信息。
  2. 你不應該在擴展Activity的類中定義公共方法以供其他類使用。如果您想將某些數據庫方法暴露給多個活動,則爲其創建一個單獨的類,然後在您的活動中調用該方法。你說LectureNoting延伸Activity。你確定嗎?如果您只是打電話給addEvent(),那麼您必須延長SaveData

無論哪種方式,請不要撥打方法從一個ACTIVITY在另一個。如果您想要將方法公開給多個活動,請在它自己的類中創建一個與您所公開的功能組相關的明智名稱。

+0

好,我期待看到什麼被延長,但我只是以爲我錯過了一些東西。 –

相關問題