[Q] App keeps crashing when starting activity - Java for Android App Development

My app keeps crashing when I try to start this activity. It is the Kit Kat easter egg. here is the code:
Code:
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Bon Appetit!",
Toast.LENGTH_LONG).show();
Intent dessert = new Intent("com.android.systemui.DessertCase");
startActivity(dessert);
}
here is the logcat:
04-06 22:57:26.607 31168-31168/com.jonanomisk.dessert E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.jonanomisk.dessert, PID: 31168
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19748)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
************at android.view.View.performClick(View.java:4756)
************at android.view.View$PerformClick.run(View.java:19748)
************at android.os.Handler.handleCallback(Handler.java:739)
************at android.os.Handler.dispatchMessage(Handler.java:95)
************at android.os.Looper.loop(Looper.java:135)
************at android.app.ActivityThread.main(ActivityThread.java:5254)
************at java.lang.reflect.Method.invoke(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:372)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.systemui.DessertCase }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.jonanomisk.dessert.MainActivity.onClick(MainActivity.java:50)
************at java.lang.reflect.Method.invoke(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:372)
************at android.view.View$1.onClick(View.java:4002)
************at android.view.View.performClick(View.java:4756)
************at android.view.View$PerformClick.run(View.java:19748)
************at android.os.Handler.handleCallback(Handler.java:739)
************at android.os.Handler.dispatchMessage(Handler.java:95)
************at android.os.Looper.loop(Looper.java:135)
************at android.app.ActivityThread.main(ActivityThread.java:5254)
************at java.lang.reflect.Method.invoke(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:372)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Click to expand...
Click to collapse
Thanks!

JonanomisK said:
My app keeps crashing when I try to start this activity. It is the Kit Kat easter egg. here is the code:
Code:
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Bon Appetit!",
Toast.LENGTH_LONG).show();
Intent dessert = new Intent("com.android.systemui.DessertCase");
startActivity(dessert);
}
here is the logcat:
Thanks!
Click to expand...
Click to collapse
Code:
at com.jonanomisk.dessert.MainActivity.onClick(MainActivity.java:50)
Line 50 is causing the issues - not sure which line it is but I can see a problem in your intent line. Try the below code:
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.systemui", "com.android.systemui.DessertCase");
startActivity(intent);

Jonny said:
Code:
at com.jonanomisk.dessert.MainActivity.onClick(MainActivity.java:50)
Line 50 is causing the issues - not sure which line it is but I can see a problem in your intent line. Try the below code:
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.systemui", "com.android.systemui.DessertCase");
startActivity(intent);
Click to expand...
Click to collapse
Thanks man that worked great!

JonanomisK said:
Thanks man that worked great!
Click to expand...
Click to collapse
Hoped it would - your problem was that you need to pass a context to the intent - usually for starting activities within your own app you can use "this" as the context - so it would be:
Code:
new Intent(this, "SomeActivity.class");
But the package that the easter egg is under is com.android.systemui so that's what you have to set the class name using that as the context:
Code:
intent.setClassName("com.android.systemui", "com.android.systemui.DessertCase");

Related

[CODE][Q] Pass extra twice(image to mms), or use lastIndexOf() to get file?

Ok so this may get sort of complicated, but I'll try to explain it simply.
I have a mainActivity(viewpager). i take a screenshot of this from ABS options menu. This gets saved to sdcard . WORKS FINE
Code:
this is selecting from the actionbar menu
else if (item.getTitle().toString().equalsIgnoreCase("Take Screengrab")) {
View v1 = R1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
Util.saveBitmap(bm, "folder", "screenshot");
//the above are the folder I chose to name, and the screenshotname{without additional numbering}
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.image4);
//imageview is above. second ativity is below
Intent intent = new Intent(this, Screenshots.class);
intent.putExtra("image", bm);
//"image" is my extra
startActivity(intent);
I send that while its saving, to an ImageView in a second(normal) activity. WORKS FINE
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screenshots);
//The ImageView where the screenshot goes
ImageView image=(ImageView)findViewById(R.id.image4);
//This "imageId" isnt used, but for some reason the Imageview wont set without it
Intent intent = getIntent();
int imageId = intent.getIntExtra("imageId", -1); // -1 would be a default value//
//this gets my extra
Bitmap bm = (Bitmap)this.getIntent().getParcelableExtra("image");
image.setImageBitmap(bm);
I have a button to share it, it opens with picture attached. SORT OF WORKING
The thing is, I want to automatically pass this same extra Bitmap and attach it to the message.
i can open a message with a known file attached.
Code:
{
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/folder/screenshot.png"));
intent.putExtra(Intent.EXTRA_TEXT,"Today");
startActivity(intent);
i tried the working in putExtra to the message, but I get a toast from the messaging app "Sorry, cannot attach this image". NO STACK ERRORS. nothign in the logs I can use.
Code:
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("image"));
intent.putExtra(Intent.EXTRA_TEXT,"Today");
startActivity(intent);
and other stuff like that that.
I currently am loading a known file into the message and it works perfect. Problem is, I wont know the name of the file, because my screenshots get saved as image1, image2, etc. So it will be different in every case.
So im wondering if its even possible to pass and extra through twice like that, or if I should look into return the latest image in that sdcard folder with like lastIndexOf(), or something similar.
basically, is there a way to simply query find and attach the highest numbered file? without a GUI(i can do select from gallery, but am trying to make it more automated).
any thoughts would be appreciated.
P.S. i know about the lint errors and things, and somewhat sloppy self taught java, but its working. only worried about the image passing.
out of ideas said:
Ok so this may get sort of complicated, but I'll try to explain it simply.
I have a mainActivity(viewpager). i take a screenshot of this from ABS options menu. This gets saved to sdcard . WORKS FINE
Code:
this is selecting from the actionbar menu
else if (item.getTitle().toString().equalsIgnoreCase("Take Screengrab")) {
View v1 = R1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
Util.saveBitmap(bm, "folder", "screenshot");
//the above are the folder I chose to name, and the screenshotname{without additional numbering}
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.image4);
//imageview is above. second ativity is below
Intent intent = new Intent(this, Screenshots.class);
intent.putExtra("image", bm);
//"image" is my extra
startActivity(intent);
I send that while its saving, to an ImageView in a second(normal) activity. WORKS FINE
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screenshots);
//The ImageView where the screenshot goes
ImageView image=(ImageView)findViewById(R.id.image4);
//This "imageId" isnt used, but for some reason the Imageview wont set without it
Intent intent = getIntent();
int imageId = intent.getIntExtra("imageId", -1); // -1 would be a default value//
//this gets my extra
Bitmap bm = (Bitmap)this.getIntent().getParcelableExtra("image");
image.setImageBitmap(bm);
I have a button to share it, it opens with picture attached. SORT OF WORKING
The thing is, I want to automatically pass this same extra Bitmap and attach it to the message.
i can open a message with a known file attached.
Code:
{
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/folder/screenshot.png"));
intent.putExtra(Intent.EXTRA_TEXT,"Today");
startActivity(intent);
i tried the working in putExtra to the message, but I get a toast from the messaging app "Sorry, cannot attach this image". NO STACK ERRORS. nothign in the logs I can use.
Code:
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("image"));
intent.putExtra(Intent.EXTRA_TEXT,"Today");
startActivity(intent);
and other stuff like that that.
I currently am loading a known file into the message and it works perfect. Problem is, I wont know the name of the file, because my screenshots get saved as image1, image2, etc. So it will be different in every case.
So im wondering if its even possible to pass and extra through twice like that, or if I should look into return the latest image in that sdcard folder with like lastIndexOf(), or something similar.
basically, is there a way to simply query find and attach the highest numbered file? without a GUI(i can do select from gallery, but am trying to make it more automated).
any thoughts would be appreciated.
P.S. i know about the lint errors and things, and somewhat sloppy self taught java, but its working. only worried about the image passing.
Click to expand...
Click to collapse
I would just pass the filename to the share intent. In your saving method return the file name and pass it to the Uri.
lastIndexOf is a bad idea. What will you do if somebody saves a file called "xyz" in the same directory? The app will send this every time. So store the filename and pass it to the Uri.
nikwen said:
I would just pass the filename to the share intent. In your saving method return the file name and pass it to the Uri.
lastIndexOf is a bad idea. What will you do if somebody saves a file called "xyz" in the same directory? The app will send this every time. So store the filename and pass it to the Uri.
Click to expand...
Click to collapse
Yeah I had originally tried going about it that, with like getFileStreamPath and things like that, tried about a dozen different things but couldnt get them working. best i can get is that "sorry cant add..." message from the messaging app, which tells me that its passing the image, but it cant attach for some unknown reason(too large doesn't seem right, cuz i can do it with the known file name).
im a dum dum a probably should put this util class
Code:
static String saveBitmap(Bitmap bitmap, String dir, String baseName) {
try {
File sdcard = Environment.getExternalStorageDirectory();
File pictureDir = new File(sdcard, dir);
pictureDir.mkdirs();
File f = null;
for (int i = 1; i < 200; ++i) {
String name = baseName + i + ".png";
f = new File(pictureDir, name);
if (!f.exists()) {
break;
}
}
if (!f.exists()) {
String name = f.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(name);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
//how to get "name" in third activity
return name;
}
} catch (Exception e) {
} finally {
/*
if (fos != null) {
fos.close();
}
*/
}
return null;
}
So I have the name there, but just cant get it over to the messaging. I'm wondering if I should take the pic and automaticaaly pin it to the MMS, and skip the imageview part?
First: Please get a logcat.
Second: This should be working if you use the right path:
Code:
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<path here>"));
intent.putExtra(Intent.EXTRA_TEXT,"Today");
startActivity(intent);
(got it from your first post)
1. This is a for instance situtation of if i try sliding the extra or something else into Uri.parse()
E/Mms/compose( 1063): DRM error in onActivityResult()!
E/Mms/media( 1063): IOException caught while opening or reading stream
E/Mms/media( 1063): java.io.FileNotFoundException: No content provider: image
But the content of image makes it into the ImageView(as "getParcelableExtra("image"), just not in the MMS, so I don't know.
2. Yeah it works Perfect when I hardcode in a file( sdcard/folder/image1.png). attaches and sends perfect.
The problem is basically I have a loose understanding of how I should be doing this, and just cant figure out how I should properly be getting it to load.
Banging my head against the wall on this one. lol. :fingers-crossed:
out of ideas said:
1. This is a for instance situtation of if i try sliding the extra or something else into Uri.parse()
E/Mms/compose( 1063): DRM error in onActivityResult()!
E/Mms/media( 1063): IOException caught while opening or reading stream
E/Mms/media( 1063): java.io.FileNotFoundException: No content provider: image
But the content of image makes it into the ImageView(as "getParcelableExtra("image"), just not in the MMS, so I don't know.
2. Yeah it works Perfect when I hardcode in a file( sdcard/folder/image1.png). attaches and sends perfect.
The problem is basically I have a loose understanding of how I should be doing this, and just cant figure out how I should properly be getting it to load.
Banging my head against the wall on this one. lol. :fingers-crossed:
Click to expand...
Click to collapse
The problem is that you do not pass the right file name. So it cannot find the file.
---------- Post added at 07:55 PM ---------- Previous post was at 07:48 PM ----------
Change it to this and tell us the log output:
Code:
static String saveBitmap(Bitmap bitmap, String dir, String baseName) {
try {
File sdcard = Environment.getExternalStorageDirectory();
File pictureDir = new File(sdcard, dir);
pictureDir.mkdirs();
File f = null;
for (int i = 1; i < 200; ++i) {
String name = baseName + i + ".png";
f = new File(pictureDir, name);
if (!f.exists()) {
break;
}
}
if (!f.exists()) {
Log.d("file", "file does not exist");
String name = f.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(name);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
//how to get "name" in third activity
if (name != null) {
Log.d("return", name);
} else {
Log.d("return", "name = null");
}
return name;
} else {
Log.d("file", "file exists");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
/*
if (fos != null) {
fos.close();
}
*/
}
Log.d("return", "null");
return null;
}
I just inserted some debugging things. Check the logcat (and post it).
Did you add this to your manifest?
Code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
nikwen said:
The problem is that you do not pass the right file name. So it cannot find the file.
I just inserted some debugging things. Check the logcat (and post it).
Did you add this to your manifest?
Code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Click to expand...
Click to collapse
i know! I just need a way to get what my Util class returns as name. then open that attached to Message. It seems like it should be a lot easier than I am making it.
i added your logs, but the only match back I get is here (I put in some extra stuff)
Code:
D/ViewRoot( 2072): Dispatching pointer MotionEvent{2b00f540 action=1 x=85.33334 y=179.98325 pressure=0.20000002 size=0.20000002} to [email protected]
D/dalvikvm( 2072): GC_EXTERNAL_ALLOC freed 82K, 45% free 3083K/5575K, external 2159K/2284K, paused 57ms
//[B]THIS RIGHT BELOW HERE IS THE FILENAME. I NEED TO RETURN IT IN THE OTHER ACTIVITY. But How?
D/return ( 2072): /mnt/sdcard/folder/screen162.png
[/B]
I/ActivityManager( 182): Starting: Intent { cmp=com.myapp/.Screenshots (has extras) } from pid 2072
E/ActivityThread( 2072): >>> handling: 101
W/WindowManager( 182): Reached focused app: AppWindowToken{2b70bbf8 token=HistoryRecord{2b28ff08 com.myapp/.Screenshots}}
W/WindowManager( 182): updateFocusedWindowLocked newFocus=null mode=0 mCurrentFocus = Window{2b332630 PopupWindow:2afbd178 paused=false}
W/WindowManager( 182): Reached focused app: AppWindowToken{2b70bbf8 token=HistoryRecord{2b28ff08 com.myapp/.Screenshots}}
W/WindowManager( 182): updateFocusedWindowLocked newFocus=Window{2b408a60 com.myapp/com.myapp.Screenshots paused=false} mode=2 mCurrentFocus = Window{2b408a60 com.myapp/com.myapp.Screenshots paused=false}
W/WindowManager( 182): updateFocusedWindowLocked newFocus=Window{2b408a60 com.myapp/com.myapp.Screenshots paused=false} mode=3 mCurrentFocus = Window{2b408a60 com.myapp/com.myapp.Screenshots paused=false}
W/WindowManager( 182): Window Window{2b320c38 com.myapp/com.myapp.MainActivity paused=false} destroying surface Surface(name=com.myapp/com.myapp.MainActivity, identity=204), session Session{2b542460 uid 10050}
W/WindowManager( 182): Window Window{2b332630 PopupWindow:2afbd178 paused=false} destroying surface Surface(name=PopupWindow:2afbd178, identity=205), session Session{2b542460 uid 10050}
I/ActivityManager( 182): Displayed com.myapp/.Screenshots: +549ms
W/WindowManager( 182): Reached focused app: AppWindowToken{2b2dfeb8 token=HistoryRecord{2b395530 com.android.mms/.ui.ComposeMessageActivity}}
D/ActivityThread( 2072): <<< done: 106
W/WindowManager( 182): updateFocusedWindowLocked newFocus=null mode=3 mCurrentFocus = null
W/WindowManager( 182): Reached focused app: AppWindowToken{2b2dfeb8 token=HistoryRecord{2b395530 com.android.mms/.ui.ComposeMessageActivity}}
W/WindowManager( 182): Reached focused app: AppWindowToken{2b2dfeb8 token=HistoryRecord{2b395530 com.android.mms/.ui.ComposeMessageActivity}}
W/WindowManager( 182): Reached focused app: AppWindowToken{2b2dfeb8 token=HistoryRecord{2b395530 com.android.mms/.ui.ComposeMessageActivity}}
W/WindowManager( 182): updateFocusedWindowLocked newFocus=null mode=3 mCurrentFocus = null
W/WindowManager( 182): Reached focused app: AppWindowToken{2b2dfeb8 token=HistoryRecord{2b395530 com.android.mms/.ui.ComposeMessageActivity}}
W/WindowManager( 182): updateFocusedWindowLocked newFocus=null mode=3 mCurrentFocus = null
I/ActivityThread( 2081): Pub com.android.mms.SuggestionsProvider:
com.android.mms.SuggestionsProvider
E/MmsCustom( 2081): Mms,MmsCustom
E/MmsCustom( 2081): Init before calling function of doPreSotred()
E/MmsCustom( 2081): mms config have been prestored before
Yeah, I took 162 screenshots so far trying to get this. lol.
And yeah I declared the right permissions and everything. It takes the screenshot, saves it, and passes it to an ImageView in a different activity perfectly.
I can open Messaging, or FB,picassa, etc. It appends the text, but no image. BUT, i can simply hit attach from the opened message, and add in whatever picture I want.
I thought about just sending people into the gallery to pick, but the android numbering convention doesn't load images in sequential order, but a sort of numerical alphabetical( it saves likes 1, 10, 11,12,13,14,15,16,17,18,19,2,20,21,22,etc). At least this is with quickpic.
So they really get jumbled up, especially with 100+ in there(1, 10,100,11,12). And the point of this segment of the app is to share the most recent image.
i also dont want to just use the currentsystem time, because a ton of numbers is too confusing if they want to find a different one.
Thanks a lot for the help.
Let's try:
Code:
[COLOR="Red"]String path = Utils.saveBitmap(bitmap, dir, baseName);[/COLOR] //replace Utils by your class name
Uri smsUri = Uri.parse("tel:0000000");
Intent intent = new Intent(Intent.ACTION_SEND, smsUri);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.[COLOR="Red"]fromFile(path)[/COLOR]);
intent.putExtra(Intent.EXTRA_TEXT,"Today");
[COLOR="Red"]intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);[/COLOR]
startActivity(intent);
Took awhile to get Eclipse to accept it, but now I get
Code:
W/System.err( 2002): java.lang.NullPointerException
W/System.err( 2002): at com.MYAPP.Util.saveBitmap(Util.java:39)
//////The top red line you suggested
W/System.err( 2002): at com.MYAPP.Screenshots.onOptionsItemSelected(Screenshots.java:79)
W/System.err( 2002): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:119)
W/System.err( 2002): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
W/System.err( 2002): at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:529)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:83)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:158)
W/System.err( 2002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
W/System.err( 2002): at android.widget.ListView.performItemClick(ListView.java:3513)
W/System.err( 2002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
W/System.err( 2002): at android.os.Handler.handleCallback(Handler.java:587)
W/System.err( 2002): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 2002): at android.os.Looper.loop(Looper.java:130)
W/System.err( 2002): at android.app.ActivityThread.main(ActivityThread.java:3822)
W/System.err( 2002): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 2002): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 2002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
W/System.err( 2002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
W/System.err( 2002): at dalvik.system.NativeStart.main(Native Method)
D/return ( 2002): null
D/AndroidRuntime( 2002): Shutting down VM
W/dalvikvm( 2002): threadid=1: thread exiting with uncaught exception (group=0x2aac4560)
E/AndroidRuntime( 2002): FATAL EXCEPTION: main
E/AndroidRuntime( 2002): java.lang.NullPointerException: uriString
E/AndroidRuntime( 2002): at android.net.Uri$StringUri.<init>(Uri.java:420)
E/AndroidRuntime( 2002): at android.net.Uri$StringUri.<init>(Uri.java:410)
E/AndroidRuntime( 2002): at android.net.Uri.parse(Uri.java:382)
//////The Changed Uri.getFile(); Line
E/AndroidRuntime( 2002): at com.MYAPP.Screenshots.onOptionsItemSelected(Screenshots.java:82)
E/AndroidRuntime( 2002): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:119)
E/AndroidRuntime( 2002): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:529)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:83)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:158)
E/AndroidRuntime( 2002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime( 2002): at android.widget.ListView.performItemClick(ListView.java:3513)
E/AndroidRuntime( 2002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime( 2002): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 2002): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2002): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 2002): at android.app.ActivityThread.main(ActivityThread.java:3822)
E/AndroidRuntime( 2002): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2002): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 2002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 2002): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 181): Force finishing activity com.MYAPP/.Screenshots
W/WindowManager( 181)
Starting to think I should just dump the whole imageview thing.
i need to figure out if i can save to SD how I am doing, and ALSO save to a cache (and overwrite other ones, so only one image persists there)
out of ideas said:
Took awhile to get Eclipse to accept it, but now I get
Code:
W/System.err( 2002): java.lang.NullPointerException
W/System.err( 2002): at com.MYAPP.Util.saveBitmap(Util.java:39)
//////The top red line you suggested
W/System.err( 2002): at com.MYAPP.Screenshots.onOptionsItemSelected(Screenshots.java:79)
W/System.err( 2002): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:119)
W/System.err( 2002): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
W/System.err( 2002): at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:529)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:83)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
W/System.err( 2002): at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:158)
W/System.err( 2002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
W/System.err( 2002): at android.widget.ListView.performItemClick(ListView.java:3513)
W/System.err( 2002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
W/System.err( 2002): at android.os.Handler.handleCallback(Handler.java:587)
W/System.err( 2002): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 2002): at android.os.Looper.loop(Looper.java:130)
W/System.err( 2002): at android.app.ActivityThread.main(ActivityThread.java:3822)
W/System.err( 2002): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 2002): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 2002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
W/System.err( 2002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
W/System.err( 2002): at dalvik.system.NativeStart.main(Native Method)
D/return ( 2002): null
D/AndroidRuntime( 2002): Shutting down VM
W/dalvikvm( 2002): threadid=1: thread exiting with uncaught exception (group=0x2aac4560)
E/AndroidRuntime( 2002): FATAL EXCEPTION: main
E/AndroidRuntime( 2002): java.lang.NullPointerException: uriString
E/AndroidRuntime( 2002): at android.net.Uri$StringUri.<init>(Uri.java:420)
E/AndroidRuntime( 2002): at android.net.Uri$StringUri.<init>(Uri.java:410)
E/AndroidRuntime( 2002): at android.net.Uri.parse(Uri.java:382)
//////The Changed Uri.getFile(); Line
E/AndroidRuntime( 2002): at com.MYAPP.Screenshots.onOptionsItemSelected(Screenshots.java:82)
E/AndroidRuntime( 2002): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:119)
E/AndroidRuntime( 2002): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:529)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:83)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
E/AndroidRuntime( 2002): at com.actionbarsherlock.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:158)
E/AndroidRuntime( 2002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime( 2002): at android.widget.ListView.performItemClick(ListView.java:3513)
E/AndroidRuntime( 2002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime( 2002): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 2002): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2002): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 2002): at android.app.ActivityThread.main(ActivityThread.java:3822)
E/AndroidRuntime( 2002): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2002): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 2002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 2002): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 181): Force finishing activity com.MYAPP/.Screenshots
W/WindowManager( 181)
Starting to think I should just dump the whole imageview thing.
i need to figure out if i can save to SD how I am doing, and ALSO save to a cache (and overwrite other ones, so only one image persists there)
Click to expand...
Click to collapse
Post your code. We cannot help you if you just say "I changed this line."
It is not getFile but fromFile.
HaHa I know, im not trying to throw puzzles out at people randomly.
But the errors in the log were from those new lines(in red)
the string path
and fromFile path ones
I may be getting somewhere with this though, i found a little section on stackoverflow that i hadn't seen in the other 30 questions about images or screenshots. :fingers-crossed:
Plus i glad I always have about 5 other projects on can work on when I get stuck.
out of ideas said:
HaHa I know, im not trying to throw puzzles out at people randomly.
But the errors in the log were from those new lines(in red)
the string path
and fromFile path ones
I may be getting somewhere with this though, i found a little section on stackoverflow that i hadn't seen in the other 30 questions about images or screenshots. :fingers-crossed:
Plus i glad I always have about 5 other projects on can work on when I get stuck.
Click to expand...
Click to collapse
The problem occurs in the saveBitmap method. For that reason the path is null and then a NPE is thrown. Check your code to save the bitmap.

Null Pointer Exception

Hello there,
I am trying to learn android application development through one of the online tutorials .I am stuck at a point and since,both java and android are new for me I can't figure out how to get out of it.Here's the code to my Startingpoint.java:
Code:
public class Startingpoint extends Activity {
int counter;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter=0;
add=(Button)findViewById(R.id.bAdd);
sub=(Button)findViewById(R.id.bSub);
display=(TextView)findViewById(R.id.display);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
display.setText("Your total is" + counter);
}
});
}
}
The following are the contents within the LineaLlayout in my main.xml file:
Code:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="25sp"
android:layout_gravity="fill_horizontal"
android:gravity="center"
android:id="@+id/display"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/bAdd"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/bAdd"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/bSub"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/bSub"
/>
And this is what the logcat has to say :
Code:
02-13 11:44:12.076: D/AndroidRuntime(2026): Shutting down VM
02-13 11:44:12.076: W/dalvikvm(2026): threadid=1: thread exiting with uncaught exception (group=0xb2d5bb08)
02-13 11:44:12.156: E/AndroidRuntime(2026): FATAL EXCEPTION: main
02-13 11:44:12.156: E/AndroidRuntime(2026): Process: com.thenewboston.rohit, PID: 2026
02-13 11:44:12.156: E/AndroidRuntime(2026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.rohit/com.thenewboston.rohit.Startingpoint}: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.os.Looper.loop(Looper.java:137)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-13 11:44:12.156: E/AndroidRuntime(2026): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 11:44:12.156: E/AndroidRuntime(2026): at java.lang.reflect.Method.invoke(Method.java:515)
02-13 11:44:12.156: E/AndroidRuntime(2026): atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 11:44:12.156: E/AndroidRuntime(2026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 11:44:12.156: E/AndroidRuntime(2026): at dalvik.system.NativeStart.main(Native Method)
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): at com.thenewboston.rohit.Startingpoint.onCreate(Startingpoint.java:21)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.Activity.performCreate(Activity.java:5243)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-13 11:44:12.156: E/AndroidRuntime(2026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-13 11:44:12.156: E/AndroidRuntime(2026): ... 11 more
Being inexperienced in java , I could not figure out what's wrong with the code.May be the exception is due to my add,sub and display being null but I am not sure.Please help me figure out what's wrong with my code and how to deal with it.
Any help is greatly appreciated.
Well, you can find the line where it crashes in the logcat:
Code:
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): at com.thenewboston.rohit.Startingpoint.onCreate([B]Startingpoint.java:21[/B])
It's line 21. Which one is that?
Have a look at my debugging tutorial here as well, mainly the part on understanding the logcat. It will probably be very helpful and save much time: http://forum.xda-developers.com/showthread.php?t=2325164
nikwen said:
Well, you can find the line where it crashes in the logcat:
Code:
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): at com.thenewboston.rohit.Startingpoint.onCreate([B]Startingpoint.java:21[/B])
It's line 21. Which one is that?
Have a look at my debugging tutorial here as well, mainly the part on understanding the logcat. It will probably be very helpful and save much time: http://forum.xda-developers.com/showthread.php?t=2325164
Click to expand...
Click to collapse
I bet they must be the findViewById(int id);
as much i trust my counting skills line 21 is calling for one the views
i still remeber the first NPE when i started android
Sent from my GT-S5302 using Tapatalk 2
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
DarkMethod said:
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
Click to expand...
Click to collapse
It means the.view with id bAdd was not found make sure have declared it in your layout
Sent from my GT-S5302 using Tapatalk 2
I have declared the ids for each view as you can see in the layout.xml attached.This NPE is turning out to be quite frustrating now.
DarkMethod said:
I have declared the ids for each view as you can see in the layout.xml attached.This NPE is turning out to be quite frustrating now.
Click to expand...
Click to collapse
do one thing
add a breakpoint to each line
then debug your app if on Eclipse or Android Studio !
and watch the control flow through each line this should narrow you down to the root cause.
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
do one thing
add a breakpoint to each line
then debug your app if on Eclipse or Android Studio !
and watch the control flow through each line this should narrow you down to the root cause.
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
More on that here as well: http://forum.xda-developers.com/showthread.php?t=2325164
nikwen said:
More on that here as well: http://forum.xda-developers.com/showthread.php?t=2325164
Click to expand...
Click to collapse
indeed a great one :thumbup:
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
indeed a great one :thumbup:
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
Thanks.

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo java.lang.Nu

I am just taking baby steps in learning Android programming..I am following some tutorials for that and while studying how to implement onKeyListener I am getting an error 'java.lang.RuntimeException: Unable to instantiate activity ComponentInfo java.lang.NullPointerException'. This error is leading to another error 'adt unfortunately app has stopped' while trying to run the app on my phone. Here is the logcat:
05-15 15:55:22.087: D/AndroidRuntime(20672): Shutting down VM
05-15 15:55:22.087: W/dalvikvm(20672): threadid=1: thread exiting with uncaught exception (group=0x41ea1ba8)
05-15 15:55:22.087: E/AndroidRuntime(20672): FATAL EXCEPTION: main
05-15 15:55:22.087: E/AndroidRuntime(20672): Process: com.example.keyboardlistener, PID: 20672
05-15 15:55:22.087: E/AndroidRuntime(20672): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.keyboardlistener/com.example.keyboardlistener.MainActivity}: java.lang.NullPointerException
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.os.Looper.loop(Looper.java:136)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 15:55:22.087: E/AndroidRuntime(20672): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:55:22.087: E/AndroidRuntime(20672): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 15:55:22.087: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 15:55:22.087: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 15:55:22.087: E/AndroidRuntime(20672): at dalvik.system.NativeStart.main(Native Method)
05-15 15:55:22.087: E/AndroidRuntime(20672): Caused by: java.lang.NullPointerException
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.Activity.findViewById(Activity.java:1884)
05-15 15:55:22.087: E/AndroidRuntime(20672): at com.example.keyboardlistener.MainActivity.<init>(MainActivity.java:18)
05-15 15:55:22.087: E/AndroidRuntime(20672): at java.lang.Class.newInstanceImpl(Native Method)
05-15 15:55:22.087: E/AndroidRuntime(20672): at java.lang.Class.newInstance(Class.java:1208)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-15 15:55:22.087: E/AndroidRuntime(20672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-15 15:55:22.087: E/AndroidRuntime(20672): ... 11 more
05-15 16:00:21.837: I/dalvikvm(22483): Debugger is active
05-15 16:00:21.847: I/System.out(22483): Debugger has connected
05-15 16:00:21.857: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:22.057: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:22.257: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:22.457: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:22.657: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:22.857: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:23.057: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:23.257: I/System.out(22483): waiting for debugger to settle...
05-15 16:00:23.457: I/System.out(22483): debugger has settled (1495)
05-15 16:00:23.797: D/dalvikvm(22483): threadid=1: still suspended after undo (sc=1 dc=1)
05-15 16:09:43.367: D/AndroidRuntime(24025): Shutting down VM
05-15 16:09:43.367: W/dalvikvm(24025): threadid=1: thread exiting with uncaught exception (group=0x41ea1ba8)
05-15 16:09:43.367: E/AndroidRuntime(24025): FATAL EXCEPTION: main
05-15 16:09:43.367: E/AndroidRuntime(24025): Process: com.example.keyboardlistener, PID: 24025
05-15 16:09:43.367: E/AndroidRuntime(24025): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.keyboardlistener/com.example.keyboardlistener.MainActivity}: java.lang.NullPointerException
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.os.Looper.loop(Looper.java:136)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 16:09:43.367: E/AndroidRuntime(24025): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 16:09:43.367: E/AndroidRuntime(24025): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 16:09:43.367: E/AndroidRuntime(24025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 16:09:43.367: E/AndroidRuntime(24025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 16:09:43.367: E/AndroidRuntime(24025): at dalvik.system.NativeStart.main(Native Method)
05-15 16:09:43.367: E/AndroidRuntime(24025): Caused by: java.lang.NullPointerException
05-15 16:09:43.367: E/AndroidRuntime(24025): at com.example.keyboardlistener.MainActivity.onCreate(MainActivity.java:35)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.Activity.performCreate(Activity.java:5231)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 16:09:43.367: E/AndroidRuntime(24025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 16:09:43.367: E/AndroidRuntime(24025): ... 11 more
And here is the onCreate method:
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
final EditText ed = (EditText)findViewById(R.id.editTextUserEntry1);
final TextView tv = (TextView)findViewById(R.id.TextResults);
ed.setOnKeyListener(new OnKeyListener(){
@override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
tv.setText(ed.getText());
}
}
return false;
}
});
}
Hi,
to understand the logs, please read this guide on debugging apps. It tells you all you need to know to find your bug. (In this case, have a look at line 35, probably one of the IDs you pass to findViewById does not match the one you have your view set to in the XML layout file)
Declare tv and ed as fields and then this should work. The problem is, i think, that you only declared them temporarily inside onCreate, which doesn't let them being used in another method
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------
4d 61 73 72 65 70 75 73 20 66 74 77
Gesendet von Tapatalk
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.krishn
package com.example.krishnaupaharaa;
import java.util.ArrayList;
import beanClass.Bfs;
import beanClass.ItemVO;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class CategoryList extends Activity {
ItemVO details;
Bfs categoryDetails;
ArrayList<ItemVO> itemList;
ArrayAdapter<String> arrayAdapter;
ListView lv;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item_name);
itemList = new ArrayList<ItemVO>();
details = (ItemVO) ApplicationCache.getInstance().getValue("categoryDetails");
categoryDetails = (Bfs) ApplicationCache.getInstance().getValue("customerNameDetails");
//Log.d("TestTag", "Website address"+details.getMerchantWebsite());
//itemList = (ArrayList<ItemVO>) ApplicationCache.getInstance().getValue("customerNameDetails");
itemList = categoryDetails.getCategoryNameDetails();
//final LinkedHashMap<String,MFunctionVO> map = details.getMerchantFunctionalities();
lv = (ListView) findViewById(R.id.list1);
//String name = null;
arrayAdapter = new ArrayAdapter<String>(this,R.layout.listview_items,R.id.textView);
//arrayAdapter.add(details.getMerchantName());
//for(String s: map.keySet()){
// arrayAdapter.add(s);
//}
for (ItemVO mnvo : categoryDetails.getCategoryNameDetails())
{
Log.d("TestTag", "Name of custome care"+mnvo.getCategoryName());
arrayAdapter.add(mnvo.getCategoryName());
}
//lv.setAdapter(arrayAdapter);
lv.setAdapter(new MyAdapter(CategoryList.this,R.id.textView,itemList));
//Log.d("TestTag", "Values in details:" +details.getMerchantName());
//Log.d("TestTag", "Name in MerchantType class"+name);
lv.setOnItemClickListener(new OnItemClickListener() {
@override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ApplicationCache.getInstance().setValue("merchantSpecificDetails", itemList.get(position));
Intent merchantIntent = new Intent(CategoryList.this,MyCart.class);
startActivity(merchantIntent);
}
});
}
private class MyAdapter extends ArrayAdapter<ItemVO>
{
private Context context;
private ArrayList<ItemVO> list;
public MyAdapter (Context context, int textViewResourceId, ArrayList<ItemVO> list)
{
super(context, textViewResourceId, list);
this.context = context;
this.list = list;
}
@override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
{
LayoutInflater li = (LayoutInflater) getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.listview_items,null);
ImageView image=(ImageView) convertView.findViewById(R.id.imageView);
TextView itemName = (TextView) convertView.findViewById(R.id.textView);
String uri = "C:/Users/Deepak/workspace/KrishnaUpaharaa/res/drawable-hdpi" + list.get(position).getItemphoto();
Uri imguri = Uri.parse(uri);
Log.d("TestTag", "Inside getView"+list.get(position).getItemdescription());
image.setImageURI(imguri);
itemName.setText(list.get(position).getItemdescription());
return convertView;
}
}
}
}
Masrepus said:
Declare tv and ed as fields and then this should work. The problem is, i think, that you only declared them temporarily inside onCreate, which doesn't let them being used in another method
Click to expand...
Click to collapse
It not the problem. Both fields are final, so the values are copied to the instance of anonymous inner class via auto-generated constructor, thus both fields can be used inside of the instance of inner class.

[Q] New Dev trying to figure out what is probably a simple problem

I am getting a NullPointerException after the build but once my phone tries to launch the app. I do not understand where the problem is. Im just learning to code and this app is simply supposed to make a toast pop up Long if the checkbox is clicked and short if it isnt.
Code:
package universaltruth.toastee;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public CheckBox longToast = (CheckBox) findViewById(R.id.checkbox);
public EditText editText = (EditText) findViewById(R.id.toastText);
public int toastLength = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnlongToast();
}
public void addListenerOnlongToast(){
longToast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(((CheckBox) view).isChecked()){
toastLength = '1';
}
}
});
}
public void toastMessage(View view){
String message = editText.getText().toString();
Toast.makeText(getApplicationContext(),message, (toastLength == 0) ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LogCat:
02-08 12:48:34.718 6684-6684/universaltruth.toastee D/AndroidRuntime﹕ Shutting down VM
02-08 12:48:34.718 6684-6684/universaltruth.toastee W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4191bda0)
02-08 12:48:34.718 6684-6684/universaltruth.toastee E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: universaltruth.toastee, PID: 6684
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{universaltruth.toastee/universaltruth.toastee.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5748)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:2014)
at universaltruth.toastee.MainActivity.<init>(MainActivity.java:19)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599)
************at android.app.ActivityThread.access$900(ActivityThread.java:174)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
************at android.os.Handler.dispatchMessage(Handler.java:102)
************at android.os.Looper.loop(Looper.java:146)
************at android.app.ActivityThread.main(ActivityThread.java:5748)
************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:1291)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
************at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
************at dalvik.system.NativeStart.main(Native Method)
02-08 13:00:50.672 9121-9121/universaltruth.toastee I/SELinux﹕ Function: selinux_android_load_priority , priority [3] , priority version is VE=SEPF_SM-N910F_4.4.4_A024
02-08 13:00:50.672 9121-9121/universaltruth.toastee E/SELinux﹕ [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
02-08 13:00:50.672 9121-9121/universaltruth.toastee E/dalvikvm﹕ >>>>> Normal User
02-08 13:00:50.672 9121-9121/universaltruth.toastee E/dalvikvm﹕ >>>>> universaltruth.toastee [ userId:0 | appId:10384 ]
02-08 13:00:50.682 9121-9121/universaltruth.toastee E/SELinux﹕ [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
02-08 13:00:50.682 9121-9121/universaltruth.toastee D/dalvikvm﹕ Late-enabling CheckJNI
02-08 13:00:50.682 9121-9121/universaltruth.toastee I/libpersona﹕ KNOX_SDCARD checking this for 10384
02-08 13:00:50.682 9121-9121/universaltruth.toastee I/libpersona﹕ KNOX_SDCARD not a persona
02-08 13:00:50.752 9121-9121/universaltruth.toastee D/TimaKeyStoreProvider﹕ in addTimaSignatureService
02-08 13:00:50.762 9121-9121/universaltruth.toastee D/TimaKeyStoreProvider﹕ Cannot add TimaSignature Service, License check Failed
02-08 13:00:50.762 9121-9121/universaltruth.toastee D/ActivityThread﹕ Added TimaKesytore provider
02-08 13:00:50.922 9121-9121/universaltruth.toastee D/AndroidRuntime﹕ Shutting down VM
02-08 13:00:50.922 9121-9121/universaltruth.toastee W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4191bda0)
02-08 13:00:50.922 9121-9121/universaltruth.toastee E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: universaltruth.toastee, PID: 9121
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{universaltruth.toastee/universaltruth.toastee.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5748)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:2014)
at universaltruth.toastee.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599)
************at android.app.ActivityThread.access$900(ActivityThread.java:174)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
************at android.os.Handler.dispatchMessage(Handler.java:102)
************at android.os.Looper.loop(Looper.java:146)
************at android.app.ActivityThread.main(ActivityThread.java:5748)
************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:1291)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
************at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
************at dalvik.system.NativeStart.main(Native Method)
02-08 13:00:55.232 9121-9121/universaltruth.toastee I/Process﹕ Sending signal. PID: 9121 SIG: 9
Forgive me if I am not posting this correctly. This is my first time. I am open to any and all feedback. Thank you.
Your problem is that you initialize the longToast checkbox outside of a method. But findViewById only returns a result if called after a call to setContentView(...), which is done in onCreate. So: put your initialization code into onCreate, after setContentView and it will work fine
--------------------
Phone: Nexus 4
OS: rooted Lollipop LRX21T
Bootloader: unlocked
Recovery: TWRP 2.8.2.0
Masrepus said:
Your problem is that you initialize the longToast checkbox outside of a method. But findViewById only returns a result if called after a call to setContentView(...), which is done in onCreate. So: put your initialization code into onCreate, after setContentView and it will work fine
Click to expand...
Click to collapse
Hey Masrepus,
Thanks a bunch for answering me.
I have moved the initializations to the on create after the setContentView. I am still getting the null pointer exception. Do you have any further advice?
UniversalTruth said:
Hey Masrepus,
Thanks a bunch for answering me.
I have moved the initializations to the on create after the setContentView. I am still getting the null pointer exception. Do you have any further advice?
Click to expand...
Click to collapse
Can you edit the first post with the new code (and new logcat)?
Yes, grab a new log and post it on pastebin
Then link the log from here

How to store geofence in disk

Hey guys,
I'm having a problem figuring out a way to store the geofences that my app creates, in order to load them when I run the app again. The purpose is to show a list of geofences created by the user.
I tried to serialize them using Java's Serialization and I also tried to use Gson to convert them into a Json string. The problem with Gson it's when I try to load them, it gives me an error message saying that Geofence is not a class. (which is true, it's an interface).
logcat:
Code:
09-09 21:37:16.148 18137-18137/? D/dalvikvm﹕ Late-enabling CheckJNI
09-09 21:37:17.515 18137-18137/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
09-09 21:37:17.515 18137-18137/? W/dalvikvm﹕ VFY: unable to resolve virtual method 614: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-09 21:37:17.515 18137-18137/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-09 21:37:17.515 18137-18137/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
09-09 21:37:17.515 18137-18137/? W/dalvikvm﹕ VFY: unable to resolve virtual method 636: Landroid/content/res/TypedArray;.getType (I)I
09-09 21:37:17.515 18137-18137/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-09 21:37:18.133 18137-18137/? D/GoogleAPIMsg﹕ Construcao do Client Google
09-09 21:37:18.164 18137-18137/? D/Manager﹕ Loading Geofences...
09-09 21:37:18.203 18137-18137/? D/AndroidRuntime﹕ Shutting down VM
09-09 21:37:18.203 18137-18137/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419307c0)
09-09 21:37:18.234 18137-18137/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartlocation.wilcocsjrxxlxpto/com.smartlocation.wilcocsjrxxlxpto.HomeScreen}: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.google.android.gms.location.Geofence. Register an InstanceCreator with Gson for this type may fix this problem.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.google.android.gms.location.Geofence. Register an InstanceCreator with Gson for this type may fix this problem.
at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.google.gson.Gson.fromJson(Gson.java:696)
at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
************at android.app.ActivityThread.access$600(ActivityThread.java:153)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
************at android.os.Handler.dispatchMessage(Handler.java:99)
************at android.os.Looper.loop(Looper.java:137)
************at android.app.ActivityThread.main(ActivityThread.java:5289)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:525)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
************at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:207)
************at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
************at com.google.gson.Gson.fromJson(Gson.java:810)
************at com.google.gson.Gson.fromJson(Gson.java:775)
************at com.google.gson.Gson.fromJson(Gson.java:724)
************at com.google.gson.Gson.fromJson(Gson.java:696)
************at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
************at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
************at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
************at android.app.Activity.performCreate(Activity.java:5133)
************at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
************at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
************at android.app.ActivityThread.access$600(ActivityThread.java:153)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
************at android.os.Handler.dispatchMessage(Handler.java:99)
************at android.os.Looper.loop(Looper.java:137)
************at android.app.ActivityThread.main(ActivityThread.java:5289)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:525)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
************at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.google.android.gms.location.Geofence; abstract class or interface
at sun.misc.Unsafe.allocateInstance(Native Method)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:525)
************at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
************at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:207)
************at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
************at com.google.gson.Gson.fromJson(Gson.java:810)
************at com.google.gson.Gson.fromJson(Gson.java:775)
************at com.google.gson.Gson.fromJson(Gson.java:724)
************at com.google.gson.Gson.fromJson(Gson.java:696)
************at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
************at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
************at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
************at android.app.Activity.performCreate(Activity.java:5133)
************at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
************at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
************at android.app.ActivityThread.access$600(ActivityThread.java:153)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
************at android.os.Handler.dispatchMessage(Handler.java:99)
************at android.os.Looper.loop(Looper.java:137)
************at android.app.ActivityThread.main(ActivityThread.java:5289)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java:525)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
************at dalvik.system.NativeStart.main(Native Method)
09-09 21:37:18.648 18137-18137/? I/Process﹕ Sending signal. PID: 18137 SIG: 9
code:
Code:
public class Manager implements ResultCallback<Status>, LocationListener {
// Tag para fazer debug
protected static final String MAN = "Manager";
protected static final String SMS = "eventSMS";
protected static final String RING = "eventRing";
private Context mAppContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
// Listas de Geofences
private TreeMap<String, Geofence> mGeofences;
private TreeMap<String, Event> mEvents;
private int mCounterActiveGeofences;
private PendingIntent mGeofencePendingIntent;
private Intent updateLocIntent;
/* Save */
private Gson gson;
private SharedPreferences prefs;
public Manager(Context context, GoogleApiClient googleApiClient){
mGeofences = new TreeMap<String, Geofence>();
mEvents = new TreeMap<String, Event>();
mGeofencePendingIntent = null;
mAppContext = context;
mGoogleApiClient = googleApiClient;
mCounterActiveGeofences = 0;
prefs = context.getSharedPreferences(Constants.Geofences, Context.MODE_PRIVATE);
gson = new Gson();
}
...
public void saveGeofences(){
String json;
Geofence geofence;
SharedPreferences.Editor editor = prefs.edit();
Log.d(MAN, "Saving Geofences...");
for (Map.Entry<String, Geofence> entry : mGeofences.entrySet()) {
geofence = mGeofences.get(entry.getKey());
json = gson.toJson(geofence);
editor.putString(geofence.getRequestId(), json);
Log.d(MAN, geofence.getRequestId() + " json: " + json);
}
editor.apply();
}
public void loadGeofences(){
Map<String, ?> keys = prefs.getAll();
Log.d(MAN, "Loading Geofences...");
for (Map.Entry<String, ?> entry : keys.entrySet()) {
String jsonString = prefs.getString(entry.getKey(), null);
Geofence geofence = gson.fromJson(jsonString, Geofence.class);
Log.d(MAN, geofence.getRequestId());
mGeofences.put(geofence.getRequestId(), geofence);
}
}
}
Thank you in advance.
wilcocsjr said:
Hey guys,
I'm having a problem figuring out a way to store the geofences that my app creates, in order to load them when I run the app again. The purpose is to show a list of geofences created by the user.
I tried to serialize them using Java's Serialization and I also tried to use Gson to convert them into a Json string. The problem with Gson it's when I try to load them, it gives me an error message saying that Geofence is not a class. (which is true, it's an interface).
Thank you in advance.
Click to expand...
Click to collapse
Could you show your code and show logcat read sticky thread how to post development questions
Sent from my SM-G530H using XDA Free mobile app
AndroidFire said:
Could you show your code and show logcat read sticky thread how to post development questions
Sent from my SM-G530H using XDA Free mobile app
Click to expand...
Click to collapse
Just edited the original post, sorry about that

Categories

Resources