Using Sharedpreferences in broadcastreceiver due to error - android

BroadcastReceiver's code is a separate unit SMSReceiver.java. The Receiver declared in the Manifest with an SMS Receive Intent Filter. So it is "always on". When the application is running or just in the list of last 8 application it's working fine when get incoming SMS. But later, when application is deleted from memory, and only receiver stands by, incoming SMS due to error. The fact that I use in receiver's code Sharedpreferences. When I get some information from received SMS I should save it in preferences for loading by main Activity later.
LogCat shows me a string with error.
public class SmsReceiver extends BroadcastReceiver
{
public static SharedPreferences mStatePrefs;
#Override
public void onReceive(Context context, Intent intent)
{
//... some code
SharedPreferences.Editor ed = mStatePrefs.edit(); //HERE I GET AN EXCEPTION
//... some code
}
}
LogCat log..
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): FATAL EXCEPTION: main
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): java.lang.RuntimeException: Unable to start receiver com.example.android.MyApplication.SmsReceiver: java.lang.NullPointerException
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2034)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.access$2400(ActivityThread.java:132)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.os.Looper.loop(Looper.java:143)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.main(ActivityThread.java:4268)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at dalvik.system.NativeStart.main(Native Method)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): Caused by: java.lang.NullPointerException
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at com.example.android.MyApplication.SmsReceiver.onReceive(SmsReceiver.java:198)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2019)
12-02 22:36:03.887: ERROR/AndroidRuntime(18345): ... 10 more
So it works fine when app is active or in memory. But when the receiver was left alone, the error has occurred. So what I need to do, to fix this problem? It's a block bug for my app, this receiver is main solution, a sense of program. It must work always and save information in preferences.

You never get a SharedPreference instance for mStatePrefs. Looking at just the code you show, mStatePrefs would still be null.
Try:
mStatePrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor ed = mStatePrefs.edit();

Related

Launch Error on Android Application

I keep getting this error:
12-02 17:34:55.648: E/AndroidRuntime(426): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
12-02 17:34:55.648: E/AndroidRuntime(426): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
12-02 17:34:55.648: E/AndroidRuntime(426): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
12-02 17:34:55.648: E/AndroidRuntime(426): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
I have tried unimporting and reimporting android.support.v7.ActionBarActivity
I hope someone can guide me to what the error is

android random exception when resuming Unable to start activity ComponentInfo android.content.res.Resources$NotFoundException

I have quite strange exception which appears when my app is resumed (after I switch to other apps and then return to mine) although it's not thrown always but only from time to time.
The part where exception is quite simple. I use ViewPager (v4) and have two dialog classes, first child DialogHelp:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState); [line 50]
second the parent PMDialogFragment (extends DialogFragment):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
app = (PMApplication) getActivity().getApplication();
View view = inflater.inflate(layoutId, container); [line 50]
and below logcat:
12-02 00:48:04.532: E/AndroidRuntime(22292): FATAL EXCEPTION: main
12-02 00:48:04.532: E/AndroidRuntime(22292): java.lang.RuntimeException: Unable to start activity ComponentInfo{pitersoft.pitermemoflashcards/pitermemo.android.gui.activities.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread.access$600(ActivityThread.java:126)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1155)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.os.Looper.loop(Looper.java:137)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread.main(ActivityThread.java:4482)
12-02 00:48:04.532: E/AndroidRuntime(22292): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 00:48:04.532: E/AndroidRuntime(22292): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 00:48:04.532: E/AndroidRuntime(22292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
12-02 00:48:04.532: E/AndroidRuntime(22292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
12-02 00:48:04.532: E/AndroidRuntime(22292): at dalvik.system.NativeStart.main(Native Method)
12-02 00:48:04.532: E/AndroidRuntime(22292): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.content.res.Resources.getValue(Resources.java:1041)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2191)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.content.res.Resources.getLayout(Resources.java:880)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-02 00:48:04.532: E/AndroidRuntime(22292): at pitermemo.android.gui.dialogs.PMDialogFragment.onCreateView(PMDialogFragment.java:50)
12-02 00:48:04.532: E/AndroidRuntime(22292): at pitermemo.android.gui.dialogs.DialogHelp.onCreateView(DialogHelp.java:50)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.Activity.performStart(Activity.java:4475)
12-02 00:48:04.532: E/AndroidRuntime(22292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1937)
12-02 00:48:04.532: E/AndroidRuntime(22292): ... 11 more
I really don't know what causes that and how to solve this. If it was repeated always, it would be much easier.
I'm thinking perhaps it's somehow related with WebView component which likes to make problems. Another suspicious thing is in onResume of main app I run sometimes AsyncTask. But the log doesn't suggest this.
Do you have any suggestions what can cause this? Thanks in advance. (android min sdk 11, run on device with android 4.0.4).

Sharing database between different activities

I am writing a program that it needs Activity A to write data to a database, while Activity B read data from the database. I extended SQLiteOpenHelper Class as DatabaseHelper, and I get my database in Activity A in this way:
SQLiteOpenHelper dbHelper = new DatabaseHelper(this, "classtableDatabase", null, 1);
SQLiteDatabase db = dbHelper.getWritableDatabase();
It works in Activity well.
When I try to open the database created by Activity A in Activity B in same way, it failed and I get these error messages:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cn.square2.iscut/cn.square2.iscut.ClassTableDetails}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
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:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
at cn.square2.iscut.ClassTableDetails.<init>(ClassTableDetails.java:28)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
... 11 more
What should I do to fix this issue? Thank you for your help!
I had the same problem. You don't need to do anything just make static references to the database from the activities other than your MAIN activity.

the application myapp(process com.example.package) has stopped unexpectedly please try again

I had so many troubles with my android app. it can't run but now I could make it, unfortunately, I receive the error" the application myapp(process com.example.package) has stopped unexpectedly please try again)
here is my log file
12-02 11:25:11.980: E/MediaPlayer(278): Unable to to create media player
12-02 11:25:12.290: D/MediaPlayer(278): create failed:
12-02 11:25:12.290: D/MediaPlayer(278): java.io.IOException: setDataSourceFD failed.: status=0x80000000
12-02 11:25:12.290: D/MediaPlayer(278): at android.media.MediaPlayer.setDataSource(Native Method)
12-02 11:25:12.290: D/MediaPlayer(278): at android.media.MediaPlayer.create(MediaPlayer.java:645)
12-02 11:25:12.290: D/MediaPlayer(278): at com.mohammed.watzIslam.Mymain.onCreate(Mymain.java:27)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-02 11:25:12.290: D/MediaPlayer(278): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 11:25:12.290: D/MediaPlayer(278): at android.os.Looper.loop(Looper.java:123)
12-02 11:25:12.290: D/MediaPlayer(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-02 11:25:12.290: D/MediaPlayer(278): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 11:25:12.290: D/MediaPlayer(278): at java.lang.reflect.Method.invoke(Method.java:521)
12-02 11:25:12.290: D/MediaPlayer(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-02 11:25:12.290: D/MediaPlayer(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-02 11:25:12.290: D/MediaPlayer(278): at dalvik.system.NativeStart.main(Native Method)
12-02 11:25:12.290: D/AndroidRuntime(278): Shutting down VM
12-02 11:25:12.290: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-02 11:25:12.300: E/AndroidRuntime(278): FATAL EXCEPTION: main
12-02 11:25:12.300: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohammed.watzIslam/com.mohammed.watzIslam.Mymain}: java.lang.NullPointerException
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-02 11:25:12.300: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 11:25:12.300: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
12-02 11:25:12.300: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-02 11:25:12.300: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-02 11:25:12.300: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
12-02 11:25:12.300: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
12-02 11:25:12.300: E/AndroidRuntime(278): at com.mohammed.watzIslam.Mymain.onCreate(Mymain.java:28)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 11:25:12.300: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-02 11:25:12.300: E/AndroidRuntime(278): ... 11 more
12-02 11:25:11.980: E/MediaPlayer(278): Unable to to create media player
12-02 11:25:12.290: D/MediaPlayer(278): create failed:
12-02 11:25:12.290: D/MediaPlayer(278): java.io.IOException: setDataSourceFD failed.: status=0x80000000
12-02 11:25:12.290: D/MediaPlayer(278): at android.media.MediaPlayer.setDataSource(Native Method)
My guess is that you're trying to load a media file that's not there, or the file name is just incorrect.

assetmanager has been finalized?

I'm getting that exception when I pick a wallpaper. I'm actually not sure what function is the culprit. Here's the error log:
I/ActivityManager( 1360): Starting activity: Intent { cmp=com.android.wallpaper.livepicker/.LiveWallpaperPreview (has extras) }
W/dalvikvm(29175): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
E/AndroidRuntime(29175): FATAL EXCEPTION: main
E/AndroidRuntime(29175): java.lang.IllegalStateException: AssetManager has been finalized!
E/AndroidRuntime(29175): at android.content.res.AssetManager.isUpToDate(Native Method)
E/AndroidRuntime(29175): at android.app.ActivityThread.getPackageInfo(ActivityThread.java:2350)
E/AndroidRuntime(29175): at android.app.ActivityThread.getPackageInfoNoCheck(ActivityThread.java:2337)
E/AndroidRuntime(29175): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2935)
E/AndroidRuntime(29175): at android.app.ActivityThread.access$3300(ActivityThread.java:125)
E/AndroidRuntime(29175): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
E/AndroidRuntime(29175): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(29175): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(29175): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(29175): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(29175): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(29175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
E/AndroidRuntime(29175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
E/AndroidRuntime(29175): at dalvik.system.NativeStart.main(Native Method)
I/ActivityManager( 1360): Displayed activity com.android.wallpaper.livepicker/.LiveWallpaperPreview: 331 ms (total 331 ms)
W/InputManagerService( 1360): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#44bf5a08
I/ActivityManager( 1360): Process com.bukabros.videolivewallpaper (pid 29175) has died.
W/ActivityManager( 1360): Scheduling restart of crashed service com.bukabros.videolivewallpaper/.VideoLiveWallpaper in 5000ms
I/ActivityManager( 1360): Start proc com.bukabros.videolivewallpaper for service com.bukabros.videolivewallpaper/.VideoLiveWallpaper: pid=29207 uid=10090 gids={}
The only thing I can think of is that onCreate doesn't get called in the right order when the wallpaper gets picked cuz that's where I instantiate the asset manager.
And yes, I'm using the NDK but the asset manager doesn't get used there.
Ah nevermind. The culprit was that the handle to the assetmanager was a static variable. (That was just a quick hack for something else). Making that a normal variable solved the problem.

Categories

Resources