Android DynamoDB mapper.save crash - android

I'm trying to follow the exact DynamoDB example for the Books DB (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/dynamodb_om.html), but my apps crashes at the
mapper.save(book)
step (it works if I comment out this line).
Please review below and help me figure out what is the problem. Thank you !
Here is the error message:
07-09 20:00:03.652 2458-2458/com.aegisofsoteria.aegisofsoteria D/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences
07-09 20:00:03.652 2458-2458/com.aegisofsoteria.aegisofsoteria D/CognitoCachingCredentialsProvider: No valid credentials found in SharedPreferences
07-09 20:00:03.673 2458-2458/com.aegisofsoteria.aegisofsoteria D/AndroidRuntime: Shutting down VM
07-09 20:00:03.674 2458-2458/com.aegisofsoteria.aegisofsoteria E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aegisofsoteria.aegisofsoteria, PID: 2458
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aegisofsoteria.aegisofsoteria/com.aegisofsoteria.aegisofsoteria.SignInActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:128)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:356)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:558)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:444)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:172)
at com.amazonaws.auth.CognitoCredentialsProvider.getIdentityId(CognitoCredentialsProvider.java:340)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getIdentityId(CognitoCachingCredentialsProvider.java:422)
at com.aegisofsoteria.aegisofsoteria.SignInActivity.BookDBExample(SignInActivity.java:203)
at com.aegisofsoteria.aegisofsoteria.SignInActivity.onStart(SignInActivity.java:124)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Here is my code:
it is inside
public class SignInActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
and the code is as following :
private void BookDBExample(){
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
my_PollID_here, // Identity Pool ID
Regions.US_EAST_1 // Region
);
Toast.makeText(getApplicationContext(), "my ID" + credentialsProvider.getIdentityId(), Toast.LENGTH_SHORT).show();
AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
Book book = new Book();
book.setTitle("Great Expectations");
book.setAuthor("Charles Dickens");
book.setPrice(1299);
book.setIsbn("1234567890");
book.setHardCover(false);
try {
mapper.save(book);
} catch (AmazonServiceException ase) {
// The conditional check failed.
Toast.makeText(getApplicationContext(), "ASE:" + ase.getMessage(), Toast.LENGTH_LONG).show();
} catch (AmazonClientException ace){
Toast.makeText(getApplicationContext(), "ACE:" + ace.getMessage(), Toast.LENGTH_LONG).show();
}
}
Two things that may act as hint:
even the error catch can not stop it from crash. Why ?
what's the meaning of "No valid credentials found in SharedPreferences" ? I set up the Cognito, IAM, DB exactly as the AWS example, and the only difference is that I can not find "Attach Role Policy" on IAM. The options are "Attach Policy" or "Create Role Policy". So, I have to use "Create Role Policy" to complete that specific step.
Thanks.

The root cause is NetworkOnMainThreadException. In short, you need to invoke mapper.save() from a background thread, say wrapping it in an AsyncTask.
The exception that is thrown when an application attempts to perform a
networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or
higher. Applications targeting earlier SDK versions are allowed to do
networking on their main event loop threads, but it's heavily
discouraged. See the document Designing for Responsiveness.

Related

Cannot connect my android studio application with dropbox, but code executes in Eclipse

I am currently having trouble accessing my dropbox account via android studio. I intend to use dropbox to store photos that the user uploads into my application, then store the dropbox filename inside of the database. I have tested this code on Eclipse, and it works like a charm. But I simply cannot get it to work in android studio. I assume I am missing a step somewhere in android studio... but I cannot find what that step may be. I would appreciate any help.
I have added the following to my build.gradle
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.dropbox.core:dropbox-core-sdk:3.1.1'
I have also added this to my android manifest under the package.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Here is the simple code I am attempting to execute. Note that I have my ACCESS_TOKEN stored as a final string, and it does contain the correct access token (as I mentioned previously, I can run this code (and more) perfectly in eclipse).
public void dropbox(){
DbxRequestConfig config = DbxRequestConfig.newBuilder("myapp").build();
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
FullAccount account = null;
try {
account = client.users().getCurrentAccount(); //This is where my error occurs
} catch (DbxException e) {
e.printStackTrace();
}
Log.d("test",account.getName().getDisplayName());
}
Here is the full error message I get.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sigm.fetchyourpet, PID: 8294
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getAllByName(InetAddress.java:1154)
at com.android.okhttp.Dns$1.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:26)
at com.dropbox.core.http.StandardHttpRequestor.getOutputStream(StandardHttpRequestor.java:132)
at com.dropbox.core.http.StandardHttpRequestor.access$000(StandardHttpRequestor.java:29)
at com.dropbox.core.http.StandardHttpRequestor$Uploader.<init>(StandardHttpRequestor.java:141)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:73)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:29)
at com.dropbox.core.DbxRequestUtil.startPostRaw(DbxRequestUtil.java:275)
at com.dropbox.core.v2.DbxRawClientV2$1.execute(DbxRawClientV2.java:146)
at com.dropbox.core.v2.DbxRawClientV2.executeRetriable(DbxRawClientV2.java:330)
at com.dropbox.core.v2.DbxRawClientV2.executeRetriableWithRefresh(DbxRawClientV2.java:351)
at com.dropbox.core.v2.DbxRawClientV2.rpcStyle(DbxRawClientV2.java:137)
at com.dropbox.core.v2.users.DbxUserUsersRequests.getCurrentAccount(DbxUserUsersRequests.java:120)
at com.sigm.fetchyourpet.MainActivity.dropbox(MainActivity.java:63)
at com.sigm.fetchyourpet.MainActivity.signUp(MainActivity.java:174)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
This happens because you are running an HTTP request on the main thread, the easy fix:
Thread thread = new Thread() {
#Override
public void run() {
try {
FullAccount account = client.users().getCurrentAccount();
Log.d("test",account.getName().getDisplayName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();

Pushwoosh.getInstance() error

I am getting an error when I try to update my pushwoosh applications in the Pushwoosh library.I've updated it in Gradle.
07-06 18:21:59.637 19950-19950/com.ahmet.sen E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ahmet.sen, PID: 19950
java.lang.ExceptionInInitializerError
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.pushwoosh.notification.j com.pushwoosh.c.d()' on a null object reference
at com.pushwoosh.Pushwoosh.<init>(Unknown Source)
at com.pushwoosh.Pushwoosh.<clinit>(Unknown Source)
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Where i get the error
Pushwoosh.getInstance().registerForPushNotifications();
Duplicating response from Pushwoosh Github here:
This is not a bug of Pushwoosh SDK. This is a peculiarity of multi-processor applications in Android apps environment.
When you integrate analytics tools such as LeakCanary, AppMetrica, and others, these libraries start a new process for itself. A new instance of the app is created In this process. But you do not need to listen for pushes in this separate process, so by default you should not call registerForPushNotifications() inside Application.onCreate().
Therefore there are 2 possible solutions:
Not to call registerForPushNotifications inside Application.onCreate()
If you want to call registerForPushNotifications inside Application.onCreate(), you should perform a check in application's main process:
List < ActivityManager.RunningAppProcessInfo > runningAppProcesses = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (runningAppProcesses != null && runningAppProcesses.size() != 0) {
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo: runningAppProcesses) {
boolean isCurrentProcess = runningAppProcessInfo.pid == android.os.Process.myPid();
boolean isMainProcessName = getPackageName().equals(runningAppProcessInfo.processName);
if (isCurrentProcess && isMainProcessName) {
Pushwoosh.getInstance().registerForPushNotifications(...);
break;
}
}
}

FacebookSdk not starting android

I have tried everything from tutorials, answers all around, even with examples from https://github.com/facebook/facebook-android-sdk/tree/master/samples. I have tried with test app and normal app in developers.facebook.com/apps
But i always get this error
A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
Full error:
05-31 22:19:09.798 2895-2895/jonjon.fb1 I/art: Not late-enabling -Xcheck:jni (already on)
05-31 22:19:09.868 2895-2895/jonjon.fb1 W/System: ClassLoader referenced unknown path: /data/app/jonjon.fb1-1/lib/x86_64
05-31 22:19:09.888 2895-2895/jonjon.fb1 I/FacebookInitProvider: Failed to auto initialize the Facebook SDK
A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:275)
at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:231)
at com.facebook.internal.FacebookInitProvider.onCreate(FacebookInitProvider.java:20)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-31 22:19:09.892 2895-2895/jonjon.fb1 D/AndroidRuntime: Shutting down VM
--------- beginning of crash
05-31 22:19:09.892 2895-2895/jonjon.fb1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: jonjon.fb1, PID: 2895
java.lang.RuntimeException: Unable to get provider com.facebook.accountkit.internal.AccountKitInitProvider: 500: Initialization error: 502: The App Id must be specified in the string resource file as com.facebook.sdk.ApplicationId
at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 500: Initialization error: 502: The App Id must be specified in the string resource file as com.facebook.sdk.ApplicationId
at com.facebook.accountkit.internal.Initializer.getRequiredString(Initializer.java:228)
at com.facebook.accountkit.internal.Initializer.initialize(Initializer.java:113)
at com.facebook.accountkit.internal.AccountKitController.initialize(AccountKitController.java:592)
at com.facebook.accountkit.internal.AccountKitInitProvider.onCreate(AccountKitInitProvider.java:31)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688) 
at android.app.ActivityThread.-wrap1(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-31 22:41:18.585 3969-3969/jonjon.fb1 D/AndroidRuntime: Shutting down VM
05-31 22:41:18.585 3969-3969/jonjon.fb1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: jonjon.fb1, PID: 3969
java.lang.RuntimeException: Unable to get provider com.facebook.accountkit.internal.AccountKitInitProvider: 500: Initialization error: 503: The Client Token must be specified in the string resource file as com.facebook.accountkit.ClientToken
at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 500: Initialization error: 503: The Client Token must be specified in the string resource file as com.facebook.accountkit.ClientToken
at com.facebook.accountkit.internal.Initializer.getRequiredString(Initializer.java:228)
at com.facebook.accountkit.internal.Initializer.initialize(Initializer.java:118)
at com.facebook.accountkit.internal.AccountKitController.initialize(AccountKitController.java:592)
at com.facebook.accountkit.internal.AccountKitInitProvider.onCreate(AccountKitInitProvider.java:31)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688) 
at android.app.ActivityThread.-wrap1(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize Facebook SDK
FacebookSdk.setClientToken("XXXXXXXXXXXXX");
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
} }
Add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize Facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
}
setContentView(R.layout.activity_main); }
It fails to initialize the library.
I think the solution as said in the logcat is to add a string in strings.xml with the name
com.facebook.sdk.ApplicationId
As value you must put the token
Then you can call
FacebookSdk.sdkInitialize(this);
In onCreate
Since FacebookSdk.sdkInitialize(getApplicationContext()); is deprecated. You can use these methods to initialize facebookSDK
FacebookSdk.fullyInitialize();
AppEventsLogger.activateApp(application);
It works fine

Invalid Parameter in AWS SNS Android App

I have integrated AWS SNS API in my Android app. When I open the app on my device, I get this error log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.intap.appme, PID: 20780
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.intap.appme/com.intap.appme.MainActivity}: com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: Endpoint (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: e8589d7c-dec8-55e1-807f-cd16c8794be8)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: Endpoint (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: e8589d7c-dec8-55e1-807f-cd16c8794be8)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:712)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:388)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
at com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2262)
at com.amazonaws.services.sns.AmazonSNSClient.subscribe(AmazonSNSClient.java:1256)
at com.amazonaws.mobile.push.PushManager.subscribeToTopic(PushManager.java:251)
at com.intap.appme.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
The code lines in the log are those lines:
PushManager.java:251 (The whole method)
public void subscribeToTopic(final SnsTopic topic) {
final SubscribeRequest request = new SubscribeRequest();
request.setEndpoint(endpointArn);
request.setTopicArn(topic.getTopicArn());
request.setProtocol(SNS_PROTOCOL_APPLICATION);
/* Line 251 -> */ final SubscribeResult result = sns.subscribe(request);
// update topic and save subscription in shared preferences
final String subscriptionArn = result.getSubscriptionArn();
topic.setSubscriptionArn(subscriptionArn);
sharedPreferences.edit().putString(topic.getTopicArn(), subscriptionArn).apply();
}
MainActivity.java:50
pushManager.subscribeToTopic(pushManager.getDefaultTopic());
I didn't really realized what is the invalid parameter in my app, so you could you please help me?
According to the logs, the invalid parameter is the Endpoint.
To get the endpoint you could do...
final CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest();
request.setPlatformApplicationArn(platformApplicationArn);
request.setToken(gcmToken);
final CreatePlatformEndpointResult result = snsClient.createPlatformEndpoint(request);
The endpoint can be retrieved by:
result.getEndpointArn();

Android Studio Webview with Custom CSS (jsoup)

I want to load facebook.com in a webview with custom CSS to change a few things. I use jsoup but everytime i start the app it crashes.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Document doc = Jsoup.connect("https://www.facebook.com").get();
doc.head().getElementsByTag("link").remove();
doc.head().appendElement("link").attr("rel", "stylesheet").attr("type", "text/css").attr("href", "style.css");
String htmlData = doc.outerHtml();
WebView webview = new WebView(this);
setContentView(webview);
webview.loadDataWithBaseURL("file:///android_asset/.", htmlData, "text/html", "UTF-8", null);
} catch (IOException e) {
e.printStackTrace();
}
}
Logcat output
FATAL EXCEPTION: main Process: com.example.name.firstapp, PID: 23936
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.name.firstapp/com.example.name.firstapp.MainActivity}:
android.os.NetworkOnMainThreadException at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused
by: android.os.NetworkOnMainThreadException at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431) at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at
java.net.InetAddress.getAllByName(InetAddress.java:215) at
com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at
com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at
com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at
com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at
com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at
com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at
com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) at
org.jsoup.helper.HttpConnection.get(HttpConnection.java:216) at
com.example.name.firstapp.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6237) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You are connecting to network in the main(UI) thread. So you get android.os.NetworkOnMainThreadException.
Android system wont allow that, as it blocks the UI thread. So Use some background threads like AsyncTask or IntentService to access network.

Categories

Resources