Android Studio - How do I retrieve the contents of a web page? - java

So I've been tasked with creating an android app which will be using web services frequently.
I'm new to android and java but I cant see why this isn't working. Can someone help out? At this point all I'm trying to do is log the html code of a website, in this case Google.
MainActivity.java
package com.dummies.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import com.dummies.myapplication.WebService;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String contents = WebService.getContents("http://www.google.com", "utf-8");
Log.d("Jon", contents);
setContentView(R.layout.activity_main);
}
public void invokeWS(){
}
}
WebService.java
package com.dummies.myapplication;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
class WebService {
public static String getContents(String url, String encodeType) {
URL u;
StringBuilder builder = new StringBuilder();
try {
u = new URL(url);
try {
BufferedReader theHTML = new BufferedReader(new InputStreamReader(u.openStream(), encodeType));
String thisLine;
while ((thisLine = theHTML.readLine()) != null) {
builder.append(thisLine).append("\n");
}
} catch (Exception e) {
Log.d("Jon", "Error 2");
}
} catch (MalformedURLException e) {
Log.d("Jon", "Error");
System.err.println(url + " is not a parseable URL");
System.err.println(e);
}
return builder.toString();
}
}
I keep getting the Error 2 coming up in the log, why is this? can someone help get this code working and logging the output.
Thanks!
Stack:
02-03 23:41:56.261 2297-2297/com.dummies.myapplication D/Jon: Stack Trace:
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: android.os.NetworkOnMainThreadException
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:752)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at java.net.URL.openStream(URL.java:1057)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.dummies.myapplication.WebService.getContents(WebService.java:22)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.dummies.myapplication.MainActivity.onCreate(MainActivity.java:23)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.Activity.performCreate(Activity.java:6662)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.os.Looper.loop(Looper.java:154)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
02-03 23:41:56.527 1278-1484/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
[ 02-03 23:41:56.529 2297: 2297 D/ ]
HostConnection::get() New Host Connection established 0xa4a93200, tid 2297
[ 02-03 23:41:56.530 2297: 2297 W/ ]
Process pipe failed
02-03 23:41:56.556 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
[ 02-03 23:41:56.557 2297: 2413 D/ ]
HostConnection::get() New Host Connection established 0xa4a93600, tid 2413
02-03 23:41:56.563 2297-2413/com.dummies.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-03 23:41:56.563 2297-2413/com.dummies.myapplication D/OpenGLRenderer: Swap behavior 1
02-03 23:41:56.564 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
02-03 23:41:56.572 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
02-03 23:41:56.594 2297-2413/com.dummies.myapplication E/EGL_emulation: tid 2413: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
02-03 23:41:56.594 2297-2413/com.dummies.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x98224160, error=EGL_BAD_MATCH
02-03 23:41:56.603 2271-2271/com.google.android.googlequicksearchbox:search W/art: Long monitor contention with owner Binder:2271_1 (2283) at void java.lang.Object.wait!()(Object.java:-2) waiters=0 in java.lang.Object a.a.c.get() for 470ms
02-03 23:41:56.641 1569-1592/system_process I/ActivityManager: Displayed com.dummies.myapplication/.MainActivity: +6s119ms
02-03 23:41:56.655 1569-1693/system_process I/WindowManager: Destroying surface Surface(name=com.android.launcher3/com.android.launcher3.Launcher) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.AppWindowToken.notifyAppStopped:389 com.android.server.wm.WindowManagerService.notifyAppStopped:4456 com.android.server.am.ActivityStack.activityStoppedLocked:1252 com.android.server.am.ActivityManagerService.activityStopped:6895
02-03 23:41:56.669 2271-2414/com.google.android.googlequicksearchbox:search W/ModelDownloadController: Cannot find any speech config location.

The problem is that Android doesn't allow you to execute Network Operations on the same Thread where UIOperations happen.
In order to connect to a network, you must create a different Thread using AsyncTasks or Services.
Here's a more detailed answer about Networks on Android: How to fix android.os.NetworkOnMainThreadException?

Maybe You should use Jsoup Library. It is super cool. You can find more information here.
https://jsoup.org/

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

I dont understand why I am getting a fatal exception in my onChildClick method for my expandable list view

I have an expandable list view however when I expand it and click on the child I get an error that reads
FATAL EXCEPTION: main
Process: com.claritech.simsentinelmobile, PID: 22606
java.lang.ClassCastException: android.widget.ExpandableListConnector
cannot be cast to com.xera.deviceinsight.home.ExpandableListAdapter
This is the line that is throwing the error
ExpandableListAdapter adapter = (ExpandableListAdapter)parent.getAdapter();
and this is the entire method I am trying to use an adapter to retrieve an object(details about the clicked child from server using retrofit)
private void load(View view)
{
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
System.err.println("child clicked");
Toast.makeText(getActivity(), "child clicked", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new ItemClickedEvent(SensorInformationChildFragment.TAB_CALL));
//ExpandableListAdapter adapter = (ExpandableListAdapter)parent.getAdapter();
ExpandableListAdapter adapter = (ExpandableListAdapter)parent.getAdapter();
OrganisationDeviceSensorsResult d = (OrganisationDeviceSensorsResult) adapter.getChild(groupPosition, childPosition);
d.SensorID = d.SensorID;
OrganisationDeviceSensorsResult deviceSensor = (OrganisationDeviceSensorsResult) adapter.getChild(groupPosition , childPosition);
Object contact = adapter.getChild(groupPosition , childPosition);
// OrganisationDeviceSensorsResult contact = listDataChild.get(groupPosition).getContacts().get(childPosition);
//sensorID = deviceSensor.SensorID;
//sensorID = contact.equals()
ReportingGroup.get(childPosition);
return true;
}
});
}
This is the log
01-11 11:42:30.328 22606-22606/com.claritech.simsentinelmobile D/dalvikvm: threadid=1: detach (group=0x41816ce0)
01-11 11:42:30.328 22606-22606/com.claritech.simsentinelmobile W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41816ce0)
01-11 11:42:30.328 22606-22606/com.claritech.simsentinelmobile W/dalvikvm: threadid=1: uncaught exception occurred
01-11 11:42:30.329 22606-22606/com.claritech.simsentinelmobile W/System.err: java.lang.ClassCastException: android.widget.ExpandableListConnector cannot be cast to com.xera.deviceinsight.home.ExpandableListAdapter
01-11 11:42:30.329 22606-22606/com.claritech.simsentinelmobile W/System.err: at com.xera.deviceinsight.home.CostCentreListFragment$1.onChildClick(CostCentreListFragment.java:88)
01-11 11:42:30.329 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:712)
01-11 11:42:30.330 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:651)
01-11 11:42:30.330 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
01-11 11:42:30.330 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.widget.AbsListView$3.run(AbsListView.java:3865)
01-11 11:42:30.331 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
01-11 11:42:30.331 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
01-11 11:42:30.331 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.os.Looper.loop(Looper.java:193)
01-11 11:42:30.331 22606-22606/com.claritech.simsentinelmobile W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5299)
01-11 11:42:30.332 22606-22606/com.claritech.simsentinelmobile W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-11 11:42:30.332 22606-22606/com.claritech.simsentinelmobile W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
01-11 11:42:30.332 22606-22606/com.claritech.simsentinelmobile W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
01-11 11:42:30.332 22606-22606/com.claritech.simsentinelmobile W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
01-11 11:42:30.333 22606-22606/com.claritech.simsentinelmobile W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-11 11:42:30.333 22606-22606/com.claritech.simsentinelmobile W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
01-11 11:42:30.335 22606-22606/com.claritech.simsentinelmobile E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.claritech.simsentinelmobile, PID: 22606
java.lang.ClassCastException: android.widget.ExpandableListConnector cannot be cast to com.xera.deviceinsight.home.ExpandableListAdapter
at com.xera.deviceinsight.home.CostCentreListFragment$1.onChildClick(CostCentreListFragment.java:88)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:712)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:651)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
at android.widget.AbsListView$3.run(AbsListView.java:3865)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Try using fully qualified class name in the following statement, i.e. -
listAdapter = new com.xera.deviceinsight.home.ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);

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.

The Android Studio keep telling me that cannot find the file

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
Button mBtn1 = (Button) findViewById(R.id.start_Quit_button);
mBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B1");
Intent i = new Intent(
StartActivity.this,
MainActivity.class);
startActivity(i);
}
});
Button mBtn2 = (Button) findViewById(R.id.start_Next_button);
mBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B2");
TextView username = (TextView) findViewById(R.id.login_text);
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");
if (!root.exists()) {
root.mkdir();
}
Username = username.getText().toString();
save(root, Username);
Intent i = new Intent(
StartActivity.this,
Test1_0.class);
startActivity(i);
}
});
}
public void save(File dir, String string) {
try {
File filepath = new File(dir, Username + ".txt");
FileWriter writer = new FileWriter(filepath);
writer.append(string);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is the code I have. As you can see that i want the second button to go to the next activity and save the text I get from the editText start_Next_button. And a save method to write the string into the file created.
But the when I press the button on the AVD, android studio shows this:
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: java.io.FileNotFoundException: /sdcard/ExperimentLog/Name.txt: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileWriter.<init>(FileWriter.java:42)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity.save(StartActivity.java:74)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity$2.onClick(StartActivity.java:61)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View.performClick(View.java:5198)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View$PerformClick.run(View.java:21147)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Looper.loop(Looper.java:148)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.Posix.open(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: ... 14 more
BTW I use AVD galaxy Nexus running android 6.0
Try adding a / after ExperimentLog and then mkdir() or mkdirs().
first of all the exception is about file is not exist, so you need create that folder and file if it doesn't exist on your directory.
you need a try catch block to handle FileNotFoundException.
and also you can use Files.createFile(thePath); to create file if not exist.
Just try this
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog"+".txt");
instead of
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");

Categories