Reputation: 155
I am trying to get html content using custom arrayAdapter and asyncTask implemented in fragment. but the app crashes. Below is my code.
fragment code
package com.example.mohamed.myapplication;
import android.app.Fragment;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_frag, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new Title2().execute();
}
// Title AsyncTask
private class Title2 extends AsyncTask<Void, Void, Void> {
String title2;
String airline;
Element table;
Elements myElements;
weather weather_data[] = new weather[6] ;
String url2 = "http://www.google.com";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document2 = Jsoup.connect(url2).get();
title2 = document2.title();
for (int i= 0; i < 6; i++){
weather_data[i] = new weather(title2,title2);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
ListView listView2 = (ListView) getActivity().findViewById(R.id.list2);
weatherAdapter adapter2 = new weatherAdapter(getActivity().getApplicationContext(),
R.layout.listview, weather_data);
listView2.setAdapter(adapter2);
}
}
public static SecondFragment newInstance(String text) {
SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
constructor class
package com.example.mohamed.myapplication;
public class weather {
// public int icon;
public String title;
public String description;
public weather(){
super();
}
public weather(String title, String description) {
super();
//this.icon = icon;
this.title = title;
this.description = description;
}
}
custom arrayAdapter
package com.example.mohamed.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class weatherAdapter extends ArrayAdapter<weather> {
Context context;
int layoutResourceId;
weather data[] = null;
public weatherAdapter(Context context, int layoutResourceId, weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
weatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new weatherHolder();
//holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.tvTitle);
holder.txtDescription= (TextView)row.findViewById(R.id.tvDescription);
row.setTag(holder);
}
else
{
holder = (weatherHolder)row.getTag();
}
weather Weather = data[position];
holder.txtTitle.setText(Weather.title);
holder.txtDescription.setText(Weather.description);
//holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class weatherHolder
{
//ImageView imgIcon;
TextView txtTitle;
TextView txtDescription;
}
}
and here is my logcat
12-10 05:03:14.093 1114-1114/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 53K, 4% free 3755K/3872K, paused 2ms, total 3ms
12-10 05:03:14.097 1114-1114/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 3% free 3973K/4096K, paused 2ms, total 3ms
12-10 05:03:14.101 1114-1114/com.example.mohamed.myapplication I/dalvikvm-heap﹕ Grow heap (frag case) to 6.356MB for 2536932-byte allocation
12-10 05:03:14.105 1114-1123/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 2% free 6450K/6576K, paused 4ms, total 4ms
12-10 05:03:14.197 1114-1114/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
12-10 05:03:14.197 1114-1114/com.example.mohamed.myapplication D/﹕ HostConnection::get() New Host Connection established 0xb8a99d70, tid 1114
12-10 05:03:14.205 1114-1114/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-10 05:03:14.205 1114-1114/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
12-10 05:03:14.241 1114-1114/com.example.mohamed.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
12-10 05:03:14.241 1114-1114/com.example.mohamed.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
12-10 05:03:14.245 1114-1114/com.example.mohamed.myapplication E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
12-10 05:03:14.249 1114-1114/com.example.mohamed.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
12-10 05:03:14.249 1114-1114/com.example.mohamed.myapplication E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
12-10 05:03:14.249 1114-1114/com.example.mohamed.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
12-10 05:03:34.197 1114-1128/com.example.mohamed.myapplication W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "www.androidbegin.com": No address associated with hostname
12-10 05:03:34.197 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
12-10 05:03:34.201 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-10 05:03:34.205 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-10 05:03:34.209 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-10 05:03:34.209 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-10 05:03:34.209 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-10 05:03:34.209 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)
12-10 05:03:34.213 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.example.mohamed.myapplication.FirstFragment$Title.doInBackground(FirstFragment.java:126)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at com.example.mohamed.myapplication.FirstFragment$Title.doInBackground(FirstFragment.java:105)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 05:03:34.217 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
12-10 05:03:34.221 1114-1128/com.example.mohamed.myapplication W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-10 05:03:34.221 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
12-10 05:03:34.225 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
12-10 05:03:34.225 1114-1128/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
12-10 05:03:34.225 1114-1128/com.example.mohamed.myapplication W/System.err﹕ ... 22 more
12-10 05:03:50.417 1114-1114/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 395K, 7% free 7040K/7496K, paused 3ms, total 4ms
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "fis.com.mv": No address associated with hostname
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-10 05:03:54.257 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.example.mohamed.myapplication.SecondFragment$Title2.doInBackground(SecondFragment.java:98)
12-10 05:03:54.261 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at com.example.mohamed.myapplication.SecondFragment$Title2.doInBackground(SecondFragment.java:74)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
12-10 05:03:54.265 1114-1134/com.example.mohamed.myapplication W/System.err﹕ ... 22 more
12-10 05:03:54.281 1114-1114/com.example.mohamed.myapplication D/AndroidRuntime﹕ Shutting down VM
12-10 05:03:54.281 1114-1114/com.example.mohamed.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d1eb20)
12-10 05:03:54.285 1114-1114/com.example.mohamed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mohamed.myapplication, PID: 1114
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
at com.example.mohamed.myapplication.flightAdapter.getView(flightAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16497)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
can you please look at the logcat and tell me what is wrong with the code.
is there anything wrong with this line
private class Title2 extends AsyncTask<Void, Void, Void> {
Do i haave to pass any parameter?
Thanks in advance
flightAdapter class
package com.example.mohamed.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class flightAdapter extends ArrayAdapter<flight> {
Context context;
int layoutResourceId;
flight data[] = null;
public flightAdapter(Context context, int layoutResourceId, flight[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
flightHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new flightHolder();
//holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.tvTitle);
holder.txtDescription= (TextView)row.findViewById(R.id.tvDescription);
row.setTag(holder);
}
else
{
holder = (flightHolder)row.getTag();
}
flight Flight = data[position];
holder.txtTitle.setText(Flight.title);
holder.txtDescription.setText(Flight.description);
//holder.imgIcon.setImageResource(Flight.icon);
return row;
}
static class flightHolder
{
//ImageView imgIcon;
TextView txtTitle;
TextView txtDescription;
}
}
Upvotes: 0
Views: 367
Reputation: 2290
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
at com.example.mohamed.myapplication.flightAdapter.getView(flightAdapter.java:35)
Looks like in your class "flightadapter", your trying to cast an Application to an Activity...line 35
Upvotes: 1