I want to load facebook.com in a webview with custom CSS to change a few things. I use jsoup but everytime i start the app it crashes.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Document doc = Jsoup.connect("https://www.facebook.com").get();
doc.head().getElementsByTag("link").remove();
doc.head().appendElement("link").attr("rel", "stylesheet").attr("type", "text/css").attr("href", "style.css");
String htmlData = doc.outerHtml();
WebView webview = new WebView(this);
setContentView(webview);
webview.loadDataWithBaseURL("file:///android_asset/.", htmlData, "text/html", "UTF-8", null);
} catch (IOException e) {
e.printStackTrace();
}
}
Logcat output
FATAL EXCEPTION: main Process: com.example.name.firstapp, PID: 23936
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.name.firstapp/com.example.name.firstapp.MainActivity}:
android.os.NetworkOnMainThreadException at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused
by: android.os.NetworkOnMainThreadException at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431) at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at
java.net.InetAddress.getAllByName(InetAddress.java:215) at
com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at
com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at
com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at
com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at
com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at
com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at
com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) at
org.jsoup.helper.HttpConnection.get(HttpConnection.java:216) at
com.example.name.firstapp.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6237) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You are connecting to network in the main(UI) thread. So you get android.os.NetworkOnMainThreadException.
Android system wont allow that, as it blocks the UI thread. So Use some background threads like AsyncTask or IntentService to access network.
Related
I have tried everything from tutorials, answers all around, even with examples from https://github.com/facebook/facebook-android-sdk/tree/master/samples. I have tried with test app and normal app in developers.facebook.com/apps
But i always get this error
A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
Full error:
05-31 22:19:09.798 2895-2895/jonjon.fb1 I/art: Not late-enabling -Xcheck:jni (already on)
05-31 22:19:09.868 2895-2895/jonjon.fb1 W/System: ClassLoader referenced unknown path: /data/app/jonjon.fb1-1/lib/x86_64
05-31 22:19:09.888 2895-2895/jonjon.fb1 I/FacebookInitProvider: Failed to auto initialize the Facebook SDK
A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:275)
at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:231)
at com.facebook.internal.FacebookInitProvider.onCreate(FacebookInitProvider.java:20)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-31 22:19:09.892 2895-2895/jonjon.fb1 D/AndroidRuntime: Shutting down VM
--------- beginning of crash
05-31 22:19:09.892 2895-2895/jonjon.fb1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: jonjon.fb1, PID: 2895
java.lang.RuntimeException: Unable to get provider com.facebook.accountkit.internal.AccountKitInitProvider: 500: Initialization error: 502: The App Id must be specified in the string resource file as com.facebook.sdk.ApplicationId
at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 500: Initialization error: 502: The App Id must be specified in the string resource file as com.facebook.sdk.ApplicationId
at com.facebook.accountkit.internal.Initializer.getRequiredString(Initializer.java:228)
at com.facebook.accountkit.internal.Initializer.initialize(Initializer.java:113)
at com.facebook.accountkit.internal.AccountKitController.initialize(AccountKitController.java:592)
at com.facebook.accountkit.internal.AccountKitInitProvider.onCreate(AccountKitInitProvider.java:31)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-31 22:41:18.585 3969-3969/jonjon.fb1 D/AndroidRuntime: Shutting down VM
05-31 22:41:18.585 3969-3969/jonjon.fb1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: jonjon.fb1, PID: 3969
java.lang.RuntimeException: Unable to get provider com.facebook.accountkit.internal.AccountKitInitProvider: 500: Initialization error: 503: The Client Token must be specified in the string resource file as com.facebook.accountkit.ClientToken
at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 500: Initialization error: 503: The Client Token must be specified in the string resource file as com.facebook.accountkit.ClientToken
at com.facebook.accountkit.internal.Initializer.getRequiredString(Initializer.java:228)
at com.facebook.accountkit.internal.Initializer.initialize(Initializer.java:118)
at com.facebook.accountkit.internal.AccountKitController.initialize(AccountKitController.java:592)
at com.facebook.accountkit.internal.AccountKitInitProvider.onCreate(AccountKitInitProvider.java:31)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize Facebook SDK
FacebookSdk.setClientToken("XXXXXXXXXXXXX");
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
} }
Add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize Facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
}
setContentView(R.layout.activity_main); }
It fails to initialize the library.
I think the solution as said in the logcat is to add a string in strings.xml with the name
com.facebook.sdk.ApplicationId
As value you must put the token
Then you can call
FacebookSdk.sdkInitialize(this);
In onCreate
Since FacebookSdk.sdkInitialize(getApplicationContext()); is deprecated. You can use these methods to initialize facebookSDK
FacebookSdk.fullyInitialize();
AppEventsLogger.activateApp(application);
It works fine
when i start my app with out network and call my data in my database app crash and this is my code
public void getAllMovies() {
findData = realm.where(MovieDb.class).findAll();
movieArrayList = new ArrayList(findData);
movieMethod();
}
and i make my conflagration in here
public class myApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
RealmConfiguration configuration = new RealmConfiguration
.Builder(this).name("Movie_database.realm").build();
Realm.setDefaultConfiguration(configuration);}}
and moviedb is my realm object
my app crash in line
findData = realm.where(MovieDb.class).findAll();
and this is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.massive.movieapp, PID: 4222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.massive.movieapp/com.massive.movieapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at com.massive.movieapp.FragmentForActivity.getAllMovies(FragmentForActivity.java:143)
at com.massive.movieapp.FragmentForActivity.CallNetwork(FragmentForActivity.java:62)
at com.massive.movieapp.FragmentForActivity.onCreate(FragmentForActivity.java:55)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method
The exception happens because your realm variable is null when you call the getAllMovies method.
Remember to call realm = Realm.getDefaultInstance(); previously.
For a few days I've been trying to figure out this multithreading thing on Android Studio. I've only used SwingWorker in the past and after trying several ways to get my https connection to initiate off of the main thread I'm pulling my hair out. I've tried everything from using ASyncTask to just starting a thread everytime the connection gets made, but nothing seems to work. My app crashes every time. The most recent attempt is pictured below.
Edit: It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case.
public class YelpActivity extends AppCompatActivity implements Runnable{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
Thread t = new Thread();
t.start();
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
YelpAPI test = new YelpAPI();
test.run();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Error Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tastr.tastr, PID: 3560
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at org.scribe.model.Response.<init>(Response.java:29)
at org.scribe.model.Request.doSend(Request.java:117)
at org.scribe.model.Request.send(Request.java:66)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
public class YelpActivity extends AppCompatActivity{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
new Thread(new Runnable() {
#Override
public void run() {
YelpAPI test = new YelpAPI();
test.run();
}
}).start();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Note: It's not a good practice to implement Runnable in Activity
class. Use Core Java threads like I did in the run method or Use other
Android Background mechanism like AsyncTask/IntentService.
Some time ther is not error its run without error but most of time got error in this code, I try many thing for this. I try call this code on onStart() and I try this on onActivityCreted() too. but i get same error. This is my coding for show previous chat. I got error in limit().
final ListView listView = getListView();
mChatListAdapter = new FirebaseListAdapter<>(mFirebaseRef.limit(50), Chat.class, R.layout.chat_item, R.layout.chat_item_other,
R.layout.third_layout_chat, R.layout.fourth_layout_chat, getActivity(), chat_id);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
This is error.
FATAL EXCEPTION: main Process: com.socialapp.android, PID: 8112 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{com.socialapp.android/com.socialapp.chat.ChatViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at com.socialapp.chat.ChatViewFragment.onStart(ChatViewFragment.java:609)
at android.app.Fragment.performStart(Fragment.java:2244)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1958)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 7 years ago.
I'm trying to learn how to read and send json mensages in android.
I tried to follow the 2º answer from: "simplest way to read json from a URL in java", but for some reason it failed at the connection.
Here is the code i used:
String sURL = "http://freegeoip.net/json/";
URL url = null;
try {
url = new URL(sURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection request = null;
try {
request = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
request.connect();
} catch (IOException e) {
e.printStackTrace();
}
JsonParser jp = new JsonParser();
JsonElement root = null;
try {
root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
} catch (IOException e) {
e.printStackTrace();
}
JsonObject rootobj = root.getAsJsonObject();
content.setText(rootobj.get("zip_code").getAsString());
and here are the errors i got:
10-23 08:50:36.120 12366-12366/abff.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: abff.myapplication, PID: 12366
java.lang.RuntimeException: Unable to start activity ComponentInfo{abff.myapplication/abff.myapplication.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at abff.myapplication.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
If you read the error you will see it says: android.os.NetworkOnMainThreadException. You can't perform http requests on the main thread. Make sure you use another thread or an AsyncTask