How to store geofence in disk - Java for Android App Development

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

Related

freeze on unlock??

hey guys it's been like 2 days no when i try to unlock my phone the screen freezes..
sometimes i get this?
java.lang.SecurityException: Permission Denial: opening provider com.google.android.gm.provider.MailProvider from ProcessRecord{407c6ed8 1761:com.sec.android.app.twlauncher/10032} (pid=1761, uid=10032) requires com.google.android.gm.permission.READ_GMAIL or com.google.android.gm.permission.WRITE_GMAIL
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:1882)
at android.app.ActivityThread.getProvider(ActivityThread.java:3351)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:3376)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:1826)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:748)
at android.content.ContentResolver.query(ContentResolver.java:256)
at com.sec.android.app.twlauncher.Launcher.onGmailChanged(Launcher.java:30)
at com.sec.android.app.twlauncher.GmailObserver.onChange(GmailObserver.java:23)
at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

ftp4j in my app

Can anyone here help me getting ftp4j running in my app? I have written what should just connect then disconnect but it crashes the app. I have googled ALL OVER and can NOT figure out how to get it implemented. Any help appreciated.
EDIT: What I was REALLY asking for was what YOU would do to make it work, but I'll post my non functioning code I guess. FYI I did give permissions for internet.
Main.java:
package com.JKIC.pictures;
import java.io.IOException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Main extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
FTPClient ftp = new FTPClient();
ftp.connect("xxx.com);
ftp.login("xxx", "xxx");
ftp.disconnect(true);
} catch (IllegalStateException e) {
String test = e.toString();
TextView testview = (TextView) findViewById(R.id.textView1);
testview.setText(test);
} catch (IOException e) {
String test = e.toString();
TextView testview = (TextView) findViewById(R.id.textView1);
testview.setText(test);
} catch (FTPIllegalReplyException e) {
String test = e.toString();
TextView testview = (TextView) findViewById(R.id.textView1);
testview.setText(test);
} catch (FTPException e) {
String test = e.toString();
TextView testview = (TextView) findViewById(R.id.textView1);
testview.setText(test);
}
}
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat:
07-16 00:20:42.000: E/Trace(3518): error opening trace file: No such file or directory (2)
07-16 00:20:42.580: D/AndroidRuntime(3518): Shutting down VM
07-16 00:20:42.580: W/dalvikvm(3518): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-16 00:20:42.610: E/AndroidRuntime(3518): FATAL EXCEPTION: main
07-16 00:20:42.610: E/AndroidRuntime(3518): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JKIC.pictures/com.JKIC.pictures.Main}: android.os.NetworkOnMainThreadException
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.os.Looper.loop(Looper.java:137)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 00:20:42.610: E/AndroidRuntime(3518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 00:20:42.610: E/AndroidRuntime(3518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 00:20:42.610: E/AndroidRuntime(3518): at dalvik.system.NativeStart.main(Native Method)
07-16 00:20:42.610: E/AndroidRuntime(3518): Caused by: android.os.NetworkOnMainThreadException
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.net.InetAddress.getByName(InetAddress.java:289)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
07-16 00:20:42.610: E/AndroidRuntime(3518): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
07-16 00:20:42.610: E/AndroidRuntime(3518): at it.sauronsoftware.ftp4j.FTPConnector.tcpConnectForCommunicationChannel(FTPConnector.java:208)
07-16 00:20:42.610: E/AndroidRuntime(3518): at it.sauronsoftware.ftp4j.connectors.DirectConnector.connectForCommunicationChannel(DirectConnector.java:39)
07-16 00:20:42.610: E/AndroidRuntime(3518): at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1036)
07-16 00:20:42.610: E/AndroidRuntime(3518): at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1003)
07-16 00:20:42.610: E/AndroidRuntime(3518): at com.JKIC.pictures.Main.onCreate(Main.java:22)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.Activity.performCreate(Activity.java:5104)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-16 00:20:42.610: E/AndroidRuntime(3518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-16 00:20:42.610: E/AndroidRuntime(3518): ... 11 more
07-16 00:20:42.760: D/dalvikvm(3518): GC_CONCURRENT freed 234K, 12% free 2630K/2980K, paused 6ms+4ms, total 170ms
If you post your code and logcat we could help you a little better.
zalez said:
If you post your code and logcat we could help you a little better.
Click to expand...
Click to collapse
Edited first post. Thanks for the help.
The crash you are experiencing is because you are doing network stuff on the UI thread. You need to create a separate thread for the ftp part. I don't have a ftp server to try to connect to or I would write something up.
---------- Post added at 08:22 PM ---------- Previous post was at 08:17 PM ----------
This should help:
http://stackoverflow.com/questions/15163094/how-to-use-ftp4-j-to-download-a-file-from-my-server-in-android-app

wanam exposed error log

xposed log is eating all the space available.
Code:
java.lang.NoSuchFieldError: com.android.systemui.statusbar.policy.ClockCenter#mExpandedHeader
at de.robv.android.xposed.XposedHelpers.findField(XposedHelpers.java:69)
at de.robv.android.xposed.XposedHelpers.getBooleanField(XposedHelpers.java:628)
at ma.wanam.xposed.XSysUIStatusBarClockPackage$1.afterHookedMethod(Unknown Source)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:645)
at com.android.systemui.statusbar.policy.Clock.updateClock(Native Method)
at com.android.systemui.statusbar.policy.Clock$2$1.run(Clock.java:362)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
can any body know how to fix this?
bump
toinktoink said:
xposed log is eating all the space available.
Code:
java.lang.NoSuchFieldError: com.android.systemui.statusbar.policy.ClockCenter#mExpandedHeader
at de.robv.android.xposed.XposedHelpers.findField(XposedHelpers.java:69)
at de.robv.android.xposed.XposedHelpers.getBooleanField(XposedHelpers.java:628)
at ma.wanam.xposed.XSysUIStatusBarClockPackage$1.afterHookedMethod(Unknown Source)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:645)
at com.android.systemui.statusbar.policy.Clock.updateClock(Native Method)
at com.android.systemui.statusbar.policy.Clock$2$1.run(Clock.java:362)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
can any body know how to fix this?
Click to expand...
Click to collapse
Can you upload your SystemUI.apk on my main thread?

[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

[Q] App keeps crashing when starting activity

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");

Categories

Resources