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" />
Related
I'm creating my own application. I tested it on Android Lollipop and Marshmallow - everything works great. But whenever I try Android Kitkat, an error occurs. Here's error log:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newproject/com.example.newproject.Progi}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.math.BigDecimal[][]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.math.BigDecimal[][]
at com.example.newproject.Progi.onCreate(Progi.java:62)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
It looks like problem is on line 62 in "progi" class, but - as for me - everything is OK especially that on newer Android versions it works great.
public class Progi extends AppCompatActivity {
Intent intent;
Bundle liczby;
Bundle doble;
BigDecimal tablica [][];
BigDecimal punkty = new BigDecimal(-1);
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
android.content.SharedPreferences shared = android.preference.PreferenceManager.getDefaultSharedPreferences(this);
boolean warunek = shared.getBoolean("serial", false);
android.view.MenuItem menuitem = menu.getItem(0);
if(warunek)
{
menuitem.setTitle("Oceny pojedynczo");
}
else menuitem.setTitle("Oceny seryjnie");
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.firstmenu, menu);
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutprogi);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("Progi punktowe");
intent = getIntent();
liczby = intent.getBundleExtra("liczby");
doble = intent.getBundleExtra("doble");
tablica = (BigDecimal[][]) doble.getSerializable("doble"); //here is line 62
if(intent.hasExtra("max")) {
Bundle a = intent.getBundleExtra("max");
punkty = (BigDecimal) a.getSerializable("max");
}
przejscie(true);
if(punkty.compareTo(new BigDecimal(-3))==0)
{
procenty(findViewById(android.R.id.content));
}
else if(punkty.compareTo(new BigDecimal(-1))!=0)
{
liczymy(findViewById(android.R.id.content));
}}
And in case someone asking "what 'tablica' gets from intent?":
BigDecimal[][] tablica = new BigDecimal[16][3];
//here i get data from file and put in inside "tablica"
Bundle doble = new Bundle();
doble.putSerializable("doble", tablica);
Intent intent = new Intent(Starter.this, MyActivity.class);
intent.putExtra("doble", doble);
startActivity(intent);
Any idea what's wrong?
Try switching your array implementation (BigDecimal[] []) to ArrayList.
If you want to pass an Array through an intent on Android versions
prior to Android 5.0.1 (maybe some older versions work aswell, but up
to Android 4.3 it is NOT working) You'll have to work around it. You
could do this by converting your Array to an ArrayList.
Then you would be casting it something like this:
(ArrayList<ArrayList<BigDecimal>>) doble.getSerializable("doble");
Related SO answer
I am developing an app in which my requirement to select an image from the SD card and send in to IBM Waston Visual Recognition service to identify the content in the image. I am doing like this..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == MainActivity.this.RESULT_OK && null != data){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = MainActivity.this.getContentResolver().query(selectedImage,filePathColumn,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
analizarImagen(picturePath);
}
}
private void analizarImagen(String path){
File image = new File(path);
System.out.println(path);
VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
service.setApiKey("api-key");
ClassifyImagesOptions options = new ClassifyImagesOptions.Builder()
.images(image)
.build();
VisualClassification result = service.classify(options).execute();
System.out.println(result.toString());
}
But when I select an image, the application crashes
Error:
02-02 03:43:49.720 3355-3355/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/23 }} to activity {com.cucea.mauricio.visual/com.cucea.mauricio.visual.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at okhttp3.Dns$1.lookup(Dns.java:39)
at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at okhttp3.RealCall.getResponse(RealCall.java:243)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)
at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:179)
at com.cucea.mauricio.visual.MainActivity.analizarImagen(MainActivity.java:84)
at com.cucea.mauricio.visual.MainActivity.onActivityResult(MainActivity.java:68)
at android.app.Activity.dispatchActivityResult(Activity.java:5192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
You are hitting server from main(UI) thread which is not allowed in android. Call your analizarImagen() method in separate thread or async task.
Check this document:
https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
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();
}
}
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.
My problem is that when I try to read a checkbox preference state from a different activity on bootup then send a status bar notification. Then when the device boots the I get a force close error message popup then when I go into the error log I don't understand what happens.
The code for the broadcast receiver is shown below:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//this creates a reference to my preferences activity
Prefs prefsC = new Prefs();
SharedPreferences prefs = context.getSharedPreferences("Prefs", 0);
int status = Integer.parseInt(prefs.getString("bootup", "-1"));
if(status > 0){
//notifyNS is a method that sends the status bar notification
prefsC.notifyNS("", R.drawable.n);
//the setCheckedNS method is just a custom method I made to set the state of a checkbox preference
prefsC.setCheckedNS("icon", false);
}else{
prefsC.setCheckedNS("enable", false);
prefsC.setCheckedNS("icon", false);
prefsC.setCheckedNS("bootup", false);
}
}
}
So could you help me solve the issue on why it force closes on bootup. So basically what I want to do is read a checkbox preference state on bootup then send a status bar notification.
This is my error log response:
04-16 11:23:15.546: ERROR/AndroidRuntime(977): FATAL EXCEPTION: main
04-16 11:23:15.546: ERROR/AndroidRuntime(977): java.lang.RuntimeException: Unable to instantiate receiver com.brandon.labs.nsettings.receivers.notifyBootup: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2913)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.access$3200(ActivityThread.java:135)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2198)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.os.Looper.loop(Looper.java:144)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at dalvik.system.NativeStart.main(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): Caused by: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2904)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): ... 10 more
So I have no clue what to do from here.
Alright I have figured out what I have done wrong. What it is how I was connecting the context to the notificationManger construct method and Intent construct method.
Here is my new and revised code that works:
`public class BootupReceiver extends BroadcastReceiver {
private static final boolean BOOTUP_TRUE = true;
private static final String BOOTUP_KEY = "bootup";
#Override
public void onReceive(Context context, Intent intent) {
if(getBootup(context)) {
Toast toast2 = Toast.makeText(context, "getBootup", Toast.LENGTH_SHORT);
toast2.show();
NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification Notify = new Notification(R.drawable.n,
"NSettings Enabled", System.currentTimeMillis());
Notify.flags |= Notification.FLAG_NO_CLEAR;
Notify.flags |= Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
Notify.contentView = contentView;
Intent notificationIntent = new Intent(context, Toggles.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notify.contentIntent = contentIntent;
Toast toast = Toast.makeText(context, "Notify about to be sent", Toast.LENGTH_SHORT);
toast.show();
int HELO_ID = 00000;
NotifyM.notify(HELLO_ID, Notify);
Toast toast1 = Toast.makeText(context, "Notify sent", Toast.LENGTH_SHORT);
toast1.show();
}
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.brandon.labs.nsettings.NotifyService");
context.startService(serviceIntent);
}
public static boolean getBootup(Context context){
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE);
}
}
`
Since this question has gotten more than 100 views I thought it would be nice of me to post the code that works properly.
Note: I don't know why the closing curly bracket for the class isn't showing with the rest of the code it's a stackoverflow error
According to log ('java.lang.RuntimeException: Unable to instantiate receiver'), system is unable to create instance of your receiver class, because system is unable to find specified class (com.brandon.labs.nsettings.receivers.notifyBootup). I think it is probably problems with name (of receiver class) in your AndroidManifest.xml file, and it is not related to preferences.
In future, if you will get another exception, i recommend you to read carefully message of exception ;) and stack trace. Usually they contains the half of answer to your question.
And for popular mistakes - you can try to type exception text into Google (excluding some specific info such as name of class), and you will find solution very fast.
Have you added the following to your AndroidManifest.xml along with registering the BroadcastReceiver??
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />