Toast中出错,返回null

时间:2013-04-24 14:46:26

标签: android toast

我的代码就像这样

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

3 个答案:

答案 0 :(得分:2)

而不是

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

放:

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

答案 1 :(得分: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 :(得分:0)

@siva Toast.makeText有Contex的第一个参数(android.content),它可以是你在应用程序中创建的上下文。有时候通常使用getApplicationContext(),getBasecontext(),第二个参数是String,第三个参数是持续时间

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

和 由ZouZou发布的stackoverflow.com/questions/3572463/what-is-context-in-android