lockNow() in broadcast receiver - java

it seems that i get an error when i try to use LockNow() in the broadcast receiver. can anyone help me.
public class Onlockreceive extends BroadcastReceiver {
LockSettings lockactivity;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
lockactivity.mdevicepolicymanager.lockNow();
}
}
The error message :
12-31 03:14:16.322: D/AndroidRuntime(27478): Shutting down VM 12-31 03:14:16.322: W/dalvikvm(27478): threadid=1: thread exiting with uncaught exception (group=0x41b0d378) 12-31 03:14:16.332: E/AndroidRuntime(27478): FATAL EXCEPTION: main 12-31 03:14:16.332: E/AndroidRuntime(27478): java.lang.RuntimeException: Unable to start receiver com.example.settings2.Onlockreceive: java.lang.NullPointerException 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2257) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.access$1500(ActivityThread.java:138) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.os.Handler.dispatchMessage(Handler.java:99) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.os.Looper.loop(Looper.java:213) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.main(ActivityThread.java:4787) 12-31 03:14:16.332: E/AndroidRuntime(27478): at java.lang.reflect.Method.invokeNative(Native Method) 12-31 03:14:16.332: E/AndroidRuntime(27478): at java.lang.reflect.Method.invoke(Method.java:511) 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 12-31 03:14:16.332: E/AndroidRuntime(27478): at dalvik.system.NativeStart.main(Native Method) 12-31 03:14:16.332: E/AndroidRuntime(27478): Caused by: java.lang.NullPointerException 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.example.settings2.Onlockreceive.onReceive(Onlockreceive.java:15) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2250) 12-31 03:14:16.332: E/AndroidRuntime(27478): ... 10 more
My problem is similar to this one same problem.. but i don't understand how to fix it
this is the code i use to call the broadcast receiver
private void setupAlarm(int seconds,boolean s) {
// Finish the currently running activity
// MainActivity.this.finish();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), Onlockreceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
LockSettings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "Setup the alarm");
// Getting current time and add the seconds in it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, seconds);
if (s == true){
//alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
//alarmManager.setInexactRepeating(type, triggerAtMillis, intervalMillis, operation)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pendingIntent );
}else if (s == false)
{
alarmManager.cancel(pendingIntent);
}
}

Either lockactivity or mdevicepolicymanager is null. This is not surprising, as your process may well have been terminated before the BroadcastReceiver got control.
Static data members are only a cache. Do not rely upon them. And most certainly do not put an Activity in a static data member, as you are leaking a lot of memory by doing so.
You can use the Context passed into onReceive() to retrieve a DevicePolicyManager and call lockNow().
UPDATE
Here is the revised class, complete with better formatting and case:
public class OnLockReceive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
((DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE)).lockNow();
}
}

Related

Activity crashes because of Intent with ACTION_SEND

I have an Intent to share a image from an view. It works, but once I canceled it to upload it on Instagram and I received an exception.
Google didn't help...
Intent
if(!getStringName().equals(" ")) {//check if it is usefull
LinearLayout table=(LinearLayout) findViewById(R.id.table_linear);
table.setDrawingCacheEnabled(true);
Bitmap bitmap = table.getDrawingCache();
String imagepath=saveBitmap(bitmap);
//sv.setDrawingCacheEnabled(false);
Intent shareImage=new Intent();
shareImage.setAction(Intent.ACTION_SEND);
shareImage.setType("image/png");
shareImage.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
shareImage.putExtra(Intent.EXTRA_TEXT, getStringName());
shareImage.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagepath)));
startActivity(shareImage);
//new RalaAlertToast(context, getString(R.string.share_success), false);
}else {
//show message, that it's not useful
}
Stack Trace
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.ralaweb.ralaprogramme.subnetztabelle/at.ralaweb.ralaprogramme.subnetztabelle.ActivityMain}: java.lang.RuntimeException: Parcel android.os.Parcel#41f7d760: Unmarshalling unknown type code 2131558402 at offset 200
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel#41f7d760: Unmarshalling unknown type code 2131558402 at offset 200
at android.os.Parcel.readValue(Parcel.java:2080)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2363)
at android.os.Parcel.readSparseArray(Parcel.java:1735)
at android.os.Parcel.readValue(Parcel.java:2070)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
at android.os.Bundle.unparcel(Bundle.java:249)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1273)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1794)
at android.app.Activity.onRestoreInstanceState(Activity.java:948)
at android.app.Activity.performRestoreInstanceState(Activity.java:920)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
... 12 more
another thing: should I disable the drawing cache after the export?

How to mail TextView data on button click with Android-Intent?

I am pretty new to both android and NFC. I am working on an NFC related application as a college project that reads the data from tags and lists them. Although I am able to do so, I am facing problems with intent where I am supposed to mail this list to user on button click.
Can someone please tell me where I am going wrong and help with a detailed step-by-step approach. Huge thanks..!!
Here's the WebServiceActivity:
public class WebServiceActivity extends Activity {
Intent intent = getIntent();
String studlist = intent.getStringExtra("studlist");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webservice);
TextView mailtext = (TextView) findViewById(R.id.students);
TextView mailbody = (TextView) findViewById(R.id.emailtext);
mailbody.setText("Here is the list for Tranport Tracking For Today: \n" + studlist);
Button send=(Button) findViewById(R.id.emailsendbutton);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.i("Send email", "");
String[] TO = {"h.trivedi04#gmail.com"};
String[] CC = {"h.trivedi04#gmail.com", "harshit.trivedi22#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Student Transport Track For Today");
emailIntent.putExtra(Intent.EXTRA_TEXT, studlist);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(WebServiceActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
And the main activity TransportActivity has:
Button webServiceButton = (Button)this.findViewById(R.id.webServiceButton);
webServiceButton.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View view) {
Intent intent = new Intent( view.getContext(), WebServiceActivity.class);
intent.putExtra("studlist", students.getText().toString());
startActivity(intent);
}
});
Here's the LogCat for the problem:
05-01 21:37:31.267: E/AndroidRuntime(27758): FATAL EXCEPTION: main
05-01 21:37:31.267: E/AndroidRuntime(27758): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.harshit.studenttranstrack/com.harshit.studenttranstrack.WebServiceActivity}: java.lang.NullPointerException
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.access$700(ActivityThread.java:165)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.os.Looper.loop(Looper.java:137)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.main(ActivityThread.java:5455)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.reflect.Method.invoke(Method.java:525)
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-01 21:37:31.267: E/AndroidRuntime(27758): at dalvik.system.NativeStart.main(Native Method)
05-01 21:37:31.267: E/AndroidRuntime(27758): Caused by: java.lang.NullPointerException
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.harshit.studenttranstrack.WebServiceActivity.<init>(WebServiceActivity.java:22)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.Class.newInstanceImpl(Native Method)
Try it like this inside onCreate() method.
Intent intent = this.getIntent(); //in the WebServiceActivity activity
String studlist = (String) intent .getStringExtra("studlist");
You can't getIntent() before onCreate() .There's simply no Intent available at that point. I believe the same is true for anything that requires a Context.
Set these in onCreate() method
String studlist = this.getIntent().getStringExtra("studlist");

Start activity from service isn't working (Android)

I created an overlay "always on top button", which is a service HUD, and I can't start an activity screen from there, it gives the error: "Unfortunately App has stopped". In the beginning, all I used to know if there was any TouchEventwas a toast, and that toast was created, but it was created several times, so I don't know if it gives that error because this code, which is on TouchEvent body , is repeated several times too.
here is my code:
public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
Button mButton;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setId(1);
mButton.setText("Button");
mButton.setClickable(true);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getX()<mButton.getWidth() & event.getY()>0)
{
Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); //this my toast
Intent i = new Intent(); //this is my new acivity (intent)
i.setClass(HUD.this, screen.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
HUD.this.stopSelf();
}
return false;
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(this,"Click", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
System.exit(1);
return false;
}
}
So my question is, is this code on TouchEvent body being repeated several times? If it is, is that the cause of the error?
log cat:
07-20 22:11:06.962: I/Choreographer(1620): Skipped 52 frames! The application may be doing too much work on its main thread.
07-20 22:11:08.062: D/AndroidRuntime(1620): Shutting down VM
07-20 22:11:08.062: W/dalvikvm(1620): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-20 22:11:08.132: E/AndroidRuntime(1620): FATAL EXCEPTION: main
07-20 22:11:08.132: E/AndroidRuntime(1620): android.app.SuperNotCalledException: Activity {com.example.screenshot/com.example.screenshot.screen} did not call through to super.onCreate()
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Looper.loop(Looper.java:137)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-20 22:11:08.132: E/AndroidRuntime(1620): at dalvik.system.NativeStart.main(Native Method)
screen.java:
public class screen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(), "Made it", 0).show();
finish();
}
}
See android start activity from service
Intent i= new Intent(getBaseContext(), screen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
You error seems to be inside screen activity. There are many thread which might help to figure out the error that you are getting for the activity:
Error in Android "SuperNotCalledException:Activity did not call through to super.OnCreate()"
android.app.SuperNotCalledException: Activity did not call through to super.onStop()
Update
The error is because you haven't called: super.onCreate(savedInstanceState); in your screen activity's onCreate(). That should be the first thing to be called in onCreate(). Do something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//.... other stuff
}
Hope this helps.

how to find null pointer exception with logcat

I'm trying to build a small app that takes a data file in external storage and emails it. I keep getting 'null pointer exceptions' right away in logcat and then my app dies. I can't seem to locate my exception and I am wondering if there is a way to determine the line of code that is causing this. I have the MainActivity class and then a class called SendData- the code is below. I'm new at this so any help would be greatly appreciated- thank you.
private static final String TAG = "MainActivity_ErrorLog";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
public void onStart()
{
{
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
// verify the paths
String currentDBPath = "TLC_COMMON/database.db";
String backupDBPath = "database.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists())
{
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e)
{
// change the V below to E when complete
Log.v(TAG,"ERROR: Database file not created");
}
startActivity(intentgetData);
}
}
}
--new class
public class SendData extends Activity
{
private static final String TAG = "MainActivity";
/* Checks if external storage is available to read */
public boolean isExternalStorageReadable()
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
return true;
}
return false;
}
/** Called when the user clicks the Send My Data button */
public SendData(View view)
{
// Send data by email
{
File root = Environment.getExternalStorageDirectory();
// verify it is saving as this file name; also path in previous class
String fileName = "database.db";
if (root.canWrite())
{
File attachment = new File(root, fileName);
Intent email = new Intent(Intent.ACTION_SENDTO);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Exercise data");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
// is the Uri necessary?
email.putExtra(android.content.Intent.EXTRA_TEXT, Uri.fromFile(attachment));
// look at this VVV
startActivity(Intent.createChooser(email, "Send the data via Email"));}
else
{
// Change the V below to E when complete
Log.v(TAG, "Email send failed");
}
}
}
public void finish()
{
}
}
11-13 13:29:37.343: W/dalvikvm(3319): threadid=1: thread exiting with uncaught exception (group=0x418e3300)
11-13 13:29:37.343: E/AndroidRuntime(3319): FATAL EXCEPTION: main
11-13 13:29:37.343: E/AndroidRuntime(3319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.va.datasender/com.example.va.datasender.MainActivity}: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Looper.loop(Looper.java:137)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 13:29:37.343: E/AndroidRuntime(3319): at dalvik.system.NativeStart.main(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): Caused by: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.Intent.<init>(Intent.java:3301)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstanceImpl(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstance(Class.java:1319)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-13 13:29:37.343: E/AndroidRuntime(3319): ... 11 more
Find the lowest "Caused by" and continue down until you reach a line from your code:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3301)
at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The <init> means you are doing something as a class variable or inside a constructor before the Activity has a valid Context... But the specific problem is on line 36.
I would guess that you are trying to create an Intent with this. Initialize your Intent inside onCreate() instead.
Found it. Change this:
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
to:
Intent intentgetData;
and inside onCreate() (or onStart() just before calling startActivity()) add:
intentgetData = new Intent(MainActivity.this, SendData.class);
Your problem comes from line 36 on your MainActivity. Check there for the problem.
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The exeption from line number 36: at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36).
Since the error log also shows android.content.Intent.<init> after the MainActivity.java:36, I would check the value for the first parameter in the following line, when instantiating intentgetData:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
Instead of having the instatiation done there, try doing it in the onStart() method just before calling startActivity(intentgetData);:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
startActivity(intentgetData);

Notification runs then immediately crashes

I have proximity alerts set up. (ADDED CRASH LOG)
When the proximity alert fires, I want a notification to load. Then when the user clicks the notification, a new activity/form should load.
Right now, the notification runs then the app crashes. The notification text is received and displayed successfully though.
Does anyone see something wrong with my code that would cause it to crash when firing the notification?
This code sets up the proximity alert/notification
public void SetupProximityAlerts(LocationManager lm, String name, String info, double latitude, double longitude, int range)
{
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("Name", name);
intent.putExtra("Info", info);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
lm.addProximityAlert(latitude, longitude, range, -1, pi);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new MyBroadcastReceiver(), filter);
}
And here is my BroadcastReceiver which sets the data to display in the notification. The app crashes when running: nm.notify(0, n);
Everything up until nm.notify(0, n); runs successfully. I've verified this with logging.
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
String deal = (String) extras.get("Info");
Intent notificationIntent = new Intent(context, ViewTarget.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon).setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon)).setTicker(deal).setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle("Message")
.setContentText(deal);
Notification n = builder.getNotification();
n.defaults |= Notification.DEFAULT_ALL;
nm.notify(0, n);
}
}
And here is the activity I want to load when the user clicks the notification
public class ViewTarget extends ListActivity
{
#Override
public ListAdapter getListAdapter()
{
// TODO Auto-generated method stub
return super.getListAdapter();
}
#Override
public ListView getListView()
{
// TODO Auto-generated method stub
return super.getListView();
}
#Override
public void setListAdapter(ListAdapter adapter)
{
// TODO Auto-generated method stub
super.setListAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Log.v("db", "Inside ViewTarget");
}
}
Here is the crash log
04-17 21:53:52.407: E/AndroidRuntime(4717): FATAL EXCEPTION: main
04-17 21:53:52.407: E/AndroidRuntime(4717): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.kjdv.gpsVegas.MyBroadcastReceiver (has extras) } in com.kjdv.gpsVegas.MyBroadcastReceiver#44a4bc30
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.os.Handler.handleCallback(Handler.java:587)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.os.Looper.loop(Looper.java:123)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 21:53:52.407: E/AndroidRuntime(4717): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 21:53:52.407: E/AndroidRuntime(4717): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 21:53:52.407: E/AndroidRuntime(4717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 21:53:52.407: E/AndroidRuntime(4717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 21:53:52.407: E/AndroidRuntime(4717): at dalvik.system.NativeStart.main(Native Method)
04-17 21:53:52.407: E/AndroidRuntime(4717): Caused by: java.lang.SecurityException: Requires VIBRATE permission
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.os.Parcel.readException(Parcel.java:1247)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.os.Parcel.readException(Parcel.java:1235)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.NotificationManager.notify(NotificationManager.java:110)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.NotificationManager.notify(NotificationManager.java:90)
04-17 21:53:52.407: E/AndroidRuntime(4717): at com.kjdv.gpsVegas.MyBroadcastReceiver.onReceive(MyBroadcastReceiver.java:51)
04-17 21:53:52.407: E/AndroidRuntime(4717): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)
04-17 21:53:52.407: E/AndroidRuntime(4717): ... 9 more
Thanks!
Kevin
Caused by: java.lang.SecurityException: Requires VIBRATE permission
Did you forget a permission?
Caused by: java.lang.SecurityException: Requires VIBRATE permission
the above line in your crash indicate that you are using vibrate functionality then youmust add permission in manifeast file.The below is the permission needed to add
<uses-permission android:name="android.permission.VIBRATE" />

Categories

Resources