Download this Blogger Template by Clicking Here!

Ad 468 X 60

Share

Widgets

Thursday, January 19, 2012

Widgets



Hello Friends , I am developing a call blocking app in android and i also blocking my calls but when i am accessing my data base for contacts in onReceive() of broadcasting then it showing a run time exception "Null pointerException". i have tried lot of ways but it's not available.
Here is my code snap...Please check it.


My Database Handaler class

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.sax.StartElementListener;
import android.widget.Toast;

public class Transactions
{
private static Context context;
private DonotDisturbDb dncDb;
private SQLiteDatabase sqliteDb;
private Cursor data;
public Transactions(Context context)
{
this.context=context;
dncDb=new DonotDisturbDb(context);
}
public long addBlackListNumbermanually(String number,String name)
{
long x=-1;
sqliteDb=dncDb.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("incomingNumber","+91"+number);
values.put("name", name);
try
{
if(sqliteDb.isOpen())
{
x= sqliteDb.insert("blackListContacts", null, values);
}
}
catch(Exception exc){}
finally
{
sqliteDb.close();
}
return x;
}
public Cursor getBlacklist()
{
Cursor blackList=null;
try
{
sqliteDb=dncDb.getReadableDatabase();
blackList=sqliteDb.query("blackListContacts", new String[]{"incomingNumber"}, null, null, null, null, null);
while(blackList.moveToNext())
{
String number=blackList.getString(blackList.getColumnIndex("incomingNumber"));
Toast.makeText(context, number, Toast.LENGTH_SHORT).show();
}
 
}
catch(Exception exce)
{
exce.printStackTrace();
}
finally
{
sqliteDb.close();
blackList.close();
}
return blackList;
}

}

My BroadcastReceiver class where i want access my contacts list

public class BlockBlackListIncomingNumber extends BroadcastReceiver
{
private Context cont;

@Override
public void onReceive(Context context, Intent intent) 
{
Cursor cursor=null;
try
{
Transactions trans=new Transactions(context);
        cursor=trans.getBlacklist();
        if(cursor!=null)
        { 
      cursor.moveToFirst();
       int x=cursor.getCount();
      String number=cursor.getString(cursor.getColumnIndex("incomingNumber"));
      Toast.makeText(cont, "Received"+x+number, Toast.LENGTH_SHORT).show();
         }
       else
         {
             Toast.makeText(cont, "Not Received", Toast.LENGTH_SHORT).show();
          }
 }
catch(Exception exc)
{
exc.printStackTrace();
}
finally
{
cursor.close();
}
}
}

SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →

1 comments: