2013-04-24 83 views
-1

我的代碼是這樣的錯誤吐司與空

public Database(Context context) { 
    super(context, dbname, null, dbversion); 
    try{ 
     db=getWritableDatabase(); 
     // TODO Auto-generated constructor stub 
     if (db.isOpen()){ 
     Toast.makeText(null, "Database is open", Toast.LENGTH_LONG).show(); 
     } else {  
     Toast.makeText(null, "Database is Closed", Toast.LENGTH_LONG).show(); 
     } 
    } catch(Exception e) { 
     Log.e(dbname, e.getMessage());   
    } 
} 

我得到異常的烤麪包和登錄貓顯示

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.manager/com.example.manager.MainActivity}: 
java.lang.NullPointerException: println needs a message 
+1

你不能null作爲上下文,而不是null使用getApplicationContext或上下文或this或classname.this ... – 2013-04-24 14:49:24

回答

1

替換此

Toast.makeText(null, "Database is open", Toast.LENGTH_LONG).show(); 

這個

Toast toast = Toast.makeText(context, "Database is open", Toast.LENGTH_LONG); 
toast.show(); 
2

而不是

Toast.makeText(null, "Database is open", Toast.LENGTH_LONG).show(); 

地說:

Toast.makeText(context, "Database is open", Toast.LENGTH_LONG).show(); 
+0

謝謝這工作....但你能解釋我什麼地方出錯了,情境如何解決問題。 – Siva 2013-04-24 14:50:00

+0

我推薦你這個:http://stackoverflow.com/questions/3572463/what-is-context-in-android – 2013-04-24 14:54:11

+0

謝謝鄒鄒我現在正在尋找它很長一段時間,現在我明白了。 – Siva 2013-04-24 14:58:27

0

@siva Toast.makeText擁有的Contex(android.content)第一個參數,它可以在你的應用程序創建的上下文。有時getApplicationContext(),getBasecontext()也通常使用,第二個參數是字符串,第三個參數是持續時間

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

和 stackoverflow.com/questions/3572463/what-is-context-in - 由ZouZou發佈