2013-04-24 82 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发布