json volley showing error android - java

JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
Logcat
01-07 07:27:29.294 7938-7938/info.androidhive.customlistviewvolley D/MainActivity: [{"id":"g"},{"image":"http:\/\/192.168.0.101\/test\/1.png"}]
01-07 07:27:29.312 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for image
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:72)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:59)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Looper.loop(Looper.java:135)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for id
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:71)
I can get data from the database but why does it still show that there is no value for the image?
I have checked that the url is right to get the photo. The following things are the error showed in the logcat. Please give me some helps. Thank you.

You get wrong JSONObject , try this
try {
JSONObject objId = response.getJSONObject(0);
JSONObject objImage = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(objId.getString("id"));
patient.setThumbnailUrl(objImage.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}

Look at your response and try this
JSONObject obj = response.getJSONObject(0);
JSONObject obj1 = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj1.getString("image"));

Related

Firebase notification not sending from Java code

I can successfully send notifications from the Firebase console to my device, but when I try to do it from Java code, I get a very unhelpful error:
04-12 20:18:09.284 17549-17549/com.package.example W/System.err: at com.package.example.FCMNotification.pushFCMNotification(FCMNotification.java:102)
Here is my FCMNotification Class
public class FCMNotification {
public final static String AUTH_KEY_FCM = "FCM_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(String deviceToken, String title, String body)
throws IOException, JSONException {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
}
And I call this class in my activity like this
FCMNotification obj = new FCMNotification();
obj.pushFCMNotification("DEVICE_TOKEN", "TITLE", "BODY");
EDIT
My app might be having trouble connecting to firebase server itself, so I have included my manifest and Gradle:
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/globalicon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/cardview_shadow_start_color" />
<service android:name="com.package.example.PushNotification"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="com.package.example.FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Gradle:
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/io.netty.versions.properties'
exclude 'META-INF/INDEX.LIST'
}
signingConfigs {
config {
}
}
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
multiDexEnabled true
applicationId "com.gespanet.global"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 29
versionName "1.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'MissingTranslation'
disable 'ExtraTranslation'
}
dexOptions {
javaMaxHeapSize "4g"
}
repositories {
mavenCentral()
}
dependencies {
compile ('com.google.apis:google-api-services-translate:v2-rev49-1.22.0') {
exclude group: 'com.google.guava'
}
compile ('com.google.cloud:google-cloud-translate:0.11.0-alpha') {
exclude group: 'io.grpc', module: 'grpc-all'
exclude group: 'com.google.protobuf', module: 'protobuf-java'
exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
}
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.parse.bolts:bolts-tasks:1.3.0'
compile 'com.parse:parse-android:1.13.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.facebook.android:facebook-android-sdk:4.+'
}
configurations.all {
exclude group: 'org.json', module: 'json'
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
apply plugin: 'com.google.gms.google-services'
FULL ERROR LOG CAT FROM TRY/CATCHEXCEPTION:
04-12 23:31:56.027 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.047 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
To Send a Notification with Java. Implement this class:
public class FCMNotification {
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "YOUR_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(final String DeviceIdKey, final String title, final String body) throws Exception {
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
BufferedReader in = null;
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = null;
try {
url = new URL(FMCurl);
} catch (MalformedURLException e) {
Log.i("yoyoyo", "error: 1");
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
Log.i("yoyoyo", "error: 2");
e.printStackTrace();
}
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
try {
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
Log.i("yoyoyo", "error: 3");
e.printStackTrace();
}
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
try {
json.put("to",DeviceIdKey.trim());
} catch (JSONException e) {
Log.i("yoyoyo", "error: 4");
e.printStackTrace();
}
JSONObject info = new JSONObject();
try {
info.put("title", title); // Notification title
} catch (JSONException e) {
Log.i("yoyoyo", "error: 5");
e.printStackTrace();
}
try {
info.put("body", body); // Notification body
} catch (JSONException e) {
Log.i("yoyoyo", "error: 6");
e.printStackTrace();
}
try {
json.put("notification", info);
} catch (JSONException e) {
Log.i("yoyoyo", "error: 7");
e.printStackTrace();
}
OutputStreamWriter wr = null;
try {
wr = new OutputStreamWriter(conn.getOutputStream());
} catch (IOException e) {
Log.i("yoyoyo", "error: 8");
e.printStackTrace();
}
try {
wr.write(json.toString());
} catch (IOException e) {
Log.i("yoyoyo", "error: 9");
e.printStackTrace();
}
try {
wr.flush();
} catch (IOException e) {
Log.i("yoyoyo", "error: 10");
e.printStackTrace();
}
try {
conn.getInputStream();
} catch (IOException e) {
Log.i("yoyoyo", "error: 11");
e.printStackTrace();
}
return null;
}
};
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}

Microsoft Translator API- why is Translate.execute returning nothing?

I am running an app to test whether I can use Microsoft Translator API on a phone running Android API 19. However, when I press a button to get translated text, instead of translating the text "This is my dog" to French, Microsoft Translator API returns "".
UPDATE: Logcat is returning this:
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: java.lang.Exception: [microsoft-translator-api] Error retrieving translation : Permission denied (missing INTERNET permission?)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.translate.Translate.execute(Translate.java:61)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:45)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:38)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.lang.Thread.run(Thread.java:841)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:390)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:343)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 9 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at libcore.io.Posix.getaddrinfo(Native Method)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 24 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 27 more
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 442: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 231: Landroid/app/Activity;.stopLockTask ()V
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.inspiron.translat.MainActivity.access$super
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve check-cast 227 (Landroid/os/PersistableBundle;) in Lcom/example/inspiron/translat/MainActivity;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 424:
My code is:
public class MainActivity extends AppCompatActivity {
TextView subject;
Button submit;
TextView result;
String stayalive;
String translatedText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subject = (TextView) findViewById(R.id.textView);
submit = (Button) findViewById(R.id.button);
result = (TextView) findViewById(R.id.textView2);
Translate.setClientId("CLIENT ID");
Translate.setClientSecret("CLIENT SECRET");
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
class MyAsyncTask extends AsyncTask<DownloadManager.Request, Void, String> {
protected String doInBackground() {
try {
translatedText = Translate.execute("This is my dog", Language.AUTO_DETECT, Language.FRENCH);
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
// I can't do a void
}
}
#Override
protected String doInBackground(DownloadManager.Request... params) {
return null;
}
#Override
protected void onPostExecute(String s) {
if (subject != null){
subject.setText(translatedText);
Context context = getApplicationContext();
CharSequence text = translatedText;
// the Toast didn't have any text in it
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
new MyAsyncTask().execute();
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.inspiron.translat">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<uses-permission android:name="android.permission.INTERNET"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Is the error because of my code or is it because of Microsoft Translator API itself? (I am using microsoft-translator-java-api-0.6.2-jar-with-dependencies.jar from https://code.google.com/archive/p/microsoft-translator-java-api/downloads.)
UPDATE 2: I got the answer from Permission denied (missing INTERNET permission?): But permission is given. Apparently I was supposed to put the <uses-permission> tags above application.
why you need two doInBackground call? Remove one and try

android.content.pm.PackageManager$NameNotFoundException:

I am creating an app for home automation. When the app is running on smartphone(os kitkat) I am getting the exception:
android.content.pm.PackageManager$NameNotFoundException:
This is the log
09-21 17:55:25.299 2550-2550/amrita.edu.ha W/System.err: android.content.pm.PackageManager$NameNotFoundException:
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:227)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:786)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.widget.TextView.<init>(TextView.java:1323)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.widget.EditText.<init>(EditText.java:60)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at amrita.edu.ha.AddUser.onCreate(AddUser.java:55)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.Activity.performCreate(Activity.java:5292)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.os.Looper.loop(Looper.java:193)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5292)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at dalvik.system.NativeStart.main(Native Method)
The code is below
public class AddUser extends AppCompatActivity { //implements MultiSelectionSpinner.OnMultipleItemsSelectedListener
private EditText et,et1,et2,et3,et4;
private Button btn;
private Spinner spinner;
private ImageButton imgtbn;
int i=0;
List<String> list = new ArrayList<>();
List<String> mylist=new ArrayList<>();
List<Integer>nlist=new ArrayList<>();
Integer myIntArray;
HashMap<String,Integer> map=new HashMap<String, Integer>();
Object objh="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setContentView(R.layout.adduser);**
ClientResource clientResource=new ClientResource("http://10.30.9.208:8080/api/users/view_group");
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
"admin", "admin");
clientResource.get();
Response response= clientResource.getResponse();
System.out.println("Response from server is ----> "+response.getEntityAsText());
JSONObject obj= null;
try {
obj = new JSONObject(response.getEntityAsText());
String strMessage = (String) obj.get("message");
JSONArray jarray = new JSONArray(strMessage);
for (int i = 0; i < jarray.length(); i++) {
JSONObject obj1 = jarray.getJSONObject(i);
list.add((String) obj1.get("group_name"));
map.put((String)obj1.get("group_name"),(Integer)obj1.get("group_id"));
}
}catch(JSONException e){
e.printStackTrace();
}
The exception points to the statement enclosed in two stars.

NullPointerException while storing value of spinner in a string array [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am trying to create a fill in the blanks game. I have given the options to choose from in a spinner dropdown (10 spinner for 10 blanks). Till now its working. But to calculate score, I want to store the selected options in a string array. Here I am getting a NullPointerException.
package com.example.harimohan.myapplication;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class StartPage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
TextView substring1;
TextView substring2;
TextView substring3;
TextView substring4;
TextView substring5;
TextView substring6;
TextView substring7;
TextView substring8;
TextView substring9;
TextView substring10;
TextView substring11;
Spinner blank1 ;
Spinner blank2 ;
Spinner blank3 ;
Spinner blank4 ;
Spinner blank5 ;
Spinner blank6 ;
Spinner blank7 ;
Spinner blank8 ;
Spinner blank9 ;
Spinner blank10;
String[] subs = new String[20] ;
String[] words = "batsmen domestically scored ranked appearances previously outstanding announced recipient aviation".split(" ");
String[] ans = new String[10];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
blank1 = (Spinner) findViewById(R.id.Blank1);
Spinner blank2 = (Spinner) findViewById(R.id.Blank2);
Spinner blank3 = (Spinner) findViewById(R.id.Blank3);
Spinner blank4 = (Spinner) findViewById(R.id.Blank4);
Spinner blank5 = (Spinner) findViewById(R.id.Blank5);
Spinner blank6 = (Spinner) findViewById(R.id.Blank6);
Spinner blank7 = (Spinner) findViewById(R.id.Blank7);
Spinner blank8 = (Spinner) findViewById(R.id.Blank8);
Spinner blank9 = (Spinner) findViewById(R.id.Blank9);
Spinner blank10 = (Spinner) findViewById(R.id.Blank10);
blank1.setOnItemSelectedListener(this );
blank2.setOnItemSelectedListener( this);
blank3.setOnItemSelectedListener( this);
blank4.setOnItemSelectedListener( this);
blank5.setOnItemSelectedListener( this);
blank6.setOnItemSelectedListener( this);
blank7.setOnItemSelectedListener( this);
blank8.setOnItemSelectedListener( this);
blank9.setOnItemSelectedListener( this);
blank10.setOnItemSelectedListener( this);
substring1 = (TextView) findViewById(R.id.subs1);
substring2 = (TextView) findViewById(R.id.subs2);
substring3 = (TextView) findViewById(R.id.subs3);
substring4 = (TextView) findViewById(R.id.subs4);
substring5 = (TextView) findViewById(R.id.subs5);
substring6 = (TextView) findViewById(R.id.subs6);
substring7 = (TextView) findViewById(R.id.subs7);
substring8 = (TextView) findViewById(R.id.subs8);
substring9 = (TextView) findViewById(R.id.subs9);
substring10= (TextView) findViewById(R.id.subs10);
substring11= (TextView) findViewById(R.id.subs11);
new jsontask(). execute("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&explaintext=&titles=Sachin_Tendulkar&formatversion=2&format=json");
List<String> categories = new ArrayList<String>();
categories.add("Select Answer");
categories.add(words[8]);
categories.add(words[1]);
categories.add(words[4]);
categories.add(words[3]);
categories.add(words[7]);
categories.add(words[5]);
categories.add(words[6]);
categories.add(words[0]);
categories.add(words[2]);
categories.add(words[9]);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
blank1.setAdapter(dataAdapter);
blank2.setAdapter(dataAdapter);
blank3.setAdapter(dataAdapter);
blank4.setAdapter(dataAdapter);
blank5.setAdapter(dataAdapter);
blank6.setAdapter(dataAdapter);
blank7.setAdapter(dataAdapter);
blank8.setAdapter(dataAdapter);
blank9.setAdapter(dataAdapter);
blank10.setAdapter(dataAdapter);
/*ans[0] = blank1.getSelectedItem().toString();
ans[1] = blank2.getSelectedItem().toString();
ans[2] = blank3.getSelectedItem().toString();
ans[3]= blank4.getSelectedItem().toString();
ans[4] = blank5.getSelectedItem().toString();
ans[5] = blank6.getSelectedItem().toString();
ans[6] = blank7.getSelectedItem().toString();
ans[7] = blank8.getSelectedItem().toString();
ans[8] = blank9.getSelectedItem().toString();
ans[9] = blank10.getSelectedItem().toString();*/
}
public int Score( String[] WordString, String[] AnsString){
for (int j=0;j<AnsString.length;j++) {
Log.d("Ans: " ,j +" : "+ AnsString[j]);
}
int scores=0;
for (int i=0;i<WordString.length;i++){
if (WordString[i].contains(AnsString[i])) {
scores++;
}
}
Log.d("scores",Integer.toString(scores));
return scores;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
try{
Spinner spinner = (Spinner) parent;
Log.d("spinner=",Integer.toString(spinner.getId()));
Log.d("R.id.Blank1=",Integer.toString(R.id.Blank1));
Log.d("Blank1 value=",blank1.getSelectedItem().toString());
/*if(spinner.getId() == R.id.Blank1)
{*/
String ans = blank1.getSelectedItem().toString();
Log.d("ans0 ",ans);
/*}
else if(spinner.getId() == R.id.Blank2)
{
ans[1] = blank2.getSelectedItem().toString();
Log.d("ans1 ",ans[1]);
}*/
}catch (Exception exception)
{
exception.printStackTrace();
}
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item +" at "+position+""+id, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
public class jsontask extends AsyncTask<String, String, String[]> {
#Override
protected String[] doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ;
while ((line=reader.readLine())!= null) {
buffer.append(line);
}
String json = buffer.toString();
JSONObject parentObj = new JSONObject(json);
JSONObject query = parentObj.getJSONObject("query");
JSONArray pages = query.getJSONArray("pages");
JSONObject pagearray = pages.getJSONObject(0);
String extract = pagearray.getString("extract");
int lines = 0;
int pos = 0;
String print = null;
int x;
while ((pos = extract.indexOf(".", pos) + 1) != 0) {
lines++;
if(lines==10){
x= extract.indexOf("\n",pos);
print = extract.substring(0,x);
break;
}
}
String[] sentence = print.split(" ");
for (int j = 0 ; j < words.length;j++){
boolean flag= false;
for (int i = 0; i < sentence.length ; i++) {
if (sentence[i].equals(words[j])) {
/* System.out.println(words[j]+" is found at "+ i);
System.out.println("size of "+words[j]+"="+words[j].length());
*/
if (j==0){
System.out.println("end "+print.indexOf(words[j]));
System.out.println(print.substring(0,i));
subs[j]= print.substring(0,print.indexOf(words[j]));
System.out.println(print.indexOf(words[j]));
}
else{
int start = print.indexOf(words[j-1])+ words[j-1].length();
/*System.out.println(print.indexOf(words[j-1])+"+" +words[j-1].length());
System.out.println("start"+start);
System.out.println("end"+print.indexOf(words[j]));*/
subs[j]= print.substring(start,print.indexOf(words[j]));
if(j==9){
int n =print.indexOf(words[j])+ words[j].length();
subs[10]=print.substring(n);
}
}
flag= true;
break;
}
}
if (!flag ){
System.out.println("doesnt found");
}
}
return subs;
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e3) {
e3.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
substring1.setText(subs[0]);
substring2.setText(subs[1]);
substring3.setText(subs[2]);
substring4.setText(subs[3]);
substring5.setText(subs[4]);
substring6.setText(subs[5]);
substring7.setText(subs[6]);
substring8.setText(subs[7]);
substring9.setText(subs[8]);
substring10.setText(subs[9]);
substring11.setText(subs[10]);
}
}
public void gotoScorePage(View view){
Score(words,ans);
Intent nextPage=new Intent(StartPage.this,ScorePage.class);
nextPage.putExtra("Score",Score(words,ans));
startActivity(nextPage);
}
}
This is logcat
07-13 12:33:18.360 19760-19760/com.example.harimohan.myapplication D/dalvikvm: VFY: replacing opcode 0x20 at 0x0016
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492976
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.840 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492978
07-13 12:33:18.840 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.850 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.850 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492980
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492982
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492984
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492986
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492988
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication
there is :
at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
in line 174 you have choose and null value
maybe you should initilize blank1 in on create method .
You can try to check the where the null is in line 174 (the blank1.getSelectedItem() return the null value). It's helpful for you to solve the problem . Please forgive my English is poor.

Android - Programmatically Screenshot and Share to Facebook

I have button which function is to take screenshot and share it to Facebook. when i try it in my Samsung with JellyBean OS device its working perfectly, but when i try it to BlueStack with JellyBean OS and CheeryMobile with Lollipop OS things are not working, i check the Logcat and i see some error.
my code
onClick of Button call takeScreenshot();
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
sharePhotoToFacebook(imageFile);
} catch (Throwable
e.printStackTrace();
}
}
private void sharePhotoToFacebook(File imageFile){
Uri imageUri = Uri.fromFile(imageFile);
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
List<String> permissionNeeds = Arrays.asList("publish_actions");
//this loginManager helps you eliminate adding a LoginButton to your UI
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this, permissionNeeds);
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
try{
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption(scoreFinal)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, null);
final Dialog dialog = new Dialog(ActivityShare.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sucess_shared);
dialog.show();
Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}catch (Exception e){
Log.e("Error on share", String.valueOf(e));
}
}
#Override
public void onCancel() {
Log.w("OnCancel", "Canceled by user");
// System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
final Dialog dialog = new Dialog(ActivityShare.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sucess_shared);
TextView title = (TextView)dialog.findViewById(R.id.titleSuccess);
TextView desc = (TextView)dialog.findViewById(R.id.descriptionHere);
title.setText("Unsuccessfull");
desc.setText("Something's wrong, Please check your internet connection and try again.");
dialog.show();
Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Log.e("OnError", String.valueOf(exception));
// System.out.println("onError");
}
});
}
LogCat Error
01-07 23:34:36.767 2024-2024/com.sample.app W/System.err: java.io.FileNotFoundException: /mnt/sdcard/Thu Jan 07 23:34:36 SGT 2016.jpg: open failed: EINVAL (Invalid argument)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.IoBridge.open(IoBridge.java:406)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare.takeScreenshot(ActivityShare.java:225)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare.access$000(ActivityShare.java:42)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare$1.onClick(ActivityShare.java:103)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.view.View.performClick(View.java:3511)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.view.View$PerformClick.run(View.java:14105)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Handler.handleCallback(Handler.java:605)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Looper.loop(Looper.java:137)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4424)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: Caused by: libcore.io.ErrnoException: open failed: EINVAL (Invalid argument)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.Posix.open(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.IoBridge.open(IoBridge.java:390)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: ... 16 more
i have no idea about the problem.
Please help me. Thank you!!!

Categories