How to wait until thread is completed in android - java

I have this method updatePosts() that needs to run inside of a thread. it looks like this
public void updatePosts(){
new Thread(){
public void run(){
posts.addAll(pholder.fetchPosts());
System.out.println("size of posts is " + posts.size());
// UI elements should be accessed only in
// the primary thread, so we must use the
// handler here.
}
}.start();
}
It is literally updating the List called "posts". I have this in my onCreate. The problem is, since it is running in a separate thread, it does not complete before my ImageAdapter needs to use the "posts" List.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updatePosts();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, posts));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
What options do I have to ensure that the updatePosts can finish before the ImageAdapter get created?

You should use an AsyncTask or any other mechanism that lets you offload a task to a background thread with a callback to the UI on completion.
private class BackgroundTask extends AsyncTask<Void, Void, List<Post>> {
#Override
protected List<Post> doInBackground(Void... params) {
// TODO
return posts;
}
#Override
protected void onPostExecute(List<Post> posts) {
// TODO: update UI
}
}
Another option is to use RxJava,
Observable
.fromCallable(new Callable<List<Post>>() {
#Override
public List<Post> call() throws Exception {
// TODO: get posts
return posts;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(posts -> {
// TODO: update UI
}, throwable -> {
// TODO
})

I think you need to use Handler.
private ImageAdapter mAdapter;
private MyHandler mHandler;
private List<YourData> posts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GridView gridview = (GridView) findViewById(R.id.gridview);
mAdapter = new ImageAdapter(this, posts);
gridview.setAdapter(mAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
mHandler = new MyHandler(this);
updatePosts();
}
public void updatePosts(){
new Thread(){
public void run(){
posts.addAll(pholder.fetchPosts());
System.out.println("size of posts is " + posts.size());
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(1));
}
// UI elements should be accessed only in
// the primary thread, so we must use the
// handler here.
}
}.start();
}
private static class MyHandler extend Handler{
private WeakReference<Your Activity name> outer;
public MyHandler(Your Activity name activity){
outer = new WeakReference<>(activity);
}
#Override
public void handleMessage(Message msg){
switch(msg.what){
case 1:
if(outer.get()!=null){
outer.get().mAdapter.refresh(outer.get().posts);
}
break;
default:break;
}
}
}
And in your ImageAdapter,you need add a method:
public void refresh(List<YourData> dataSet){
this.dataSet = dataSet;
notifyDataSetChanged();
}

Related

How do I update a view out of an AsyncTask?

I have to create a RecyclerView which is updated every time a new item is created by my AsyncTask. So the RecyclerView is building itself up gradually.
Every Item is generated and then the Thread sleeps for a time to see the progress slower.
I tried to get the Adapter and update it with notifyDataSetChanged(), but it wont work like this. The Error I get is:
Only the original thread that created a view hierarchy can touch its
views.
Another idea was to update the adapter in my MainActivity with the use of a interface. But I dont exactly know how to do that. First I have to know if its the right way to use an interface or if there is a better way or maybe a really easy solution for my problem.
public class MainActivity extends AppCompatActivity implements ListAdapter.Listener{
static RecyclerView recyclerView;
static ListAdapter adapter;
#Override
public void click(String name) {
if(getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE){
DetailsFragment detailsFragment = (DetailsFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_details);
detailsFragment.setData(name);
}
else{
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("sorte_name", name);
startActivity(intent);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRecyclerView();
new DataContainer().execute();
}
private void initRecyclerView(){
recyclerView = findViewById(R.id.meinRecyclerView);
adapter = new ListAdapter(this, DataContainer.meineSortenListe, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
public class DataContainer extends AsyncTask<Void, Void, Void> {
public static ArrayList<String> meineSortenListe = new ArrayList<String>();
ListAdapter myAdapter;
#Override
protected void onPreExecute() {
myAdapter =(ListAdapter)MainActivity.recyclerView.getAdapter();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
for(int i= 0; i<50; i++){
meineSortenListe.add("Sorte "+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
onProgressUpdate();
}
Log.i("info", "array befüllt");
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
myAdapter.notifyDataSetChanged();
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>{
private ArrayList<String> mSorten;
private Context mContext;
private Listener listener;
public interface Listener{
void click(String name);
}
//Constructor
public ListAdapter(Context mContext, ArrayList<String> sortenListe, Listener listener) {
this.mContext = mContext;
this.mSorten = sortenListe;
this.listener=listener;
}
///////////////////////
public class ViewHolder extends RecyclerView.ViewHolder {
TextView sortenName;
LinearLayout sorteLayout;
public ViewHolder(View itemView) {
super(itemView);
sortenName = itemView.findViewById(R.id.nameSorte);
sorteLayout = itemView.findViewById(R.id.sorteLayout);
}
}
////////////////////////////////
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
holder.sortenName.setText(mSorten.get(position));
holder.sorteLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.click(mSorten.get(position));
}
});
}
#Override
public int getItemCount() {
return mSorten.size();
}
}
In Android, only the UI thread can update views. (It is possible to trigger UI updates in onPostExecute, since onPostExecute is switching to the UI thread.)
Since you are still in the doInBackground() function, you need a Handler to send a Runnable with your code to the UI thread's MessageQueue.
So in your DataContainer, change onProgressUpdate to
protected void onProgressUpdate(Void... values) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run() {
myAdapter.notifyDataSetChanged();
}
});
super.onProgressUpdate(values);
}

ViewPager doesn't update added fragments

I got a problem with updating ViewPager fragments. We need to show fragments with data to registered user, when he doesn't registered we need to show fragments with message to register. I use this method to check it in MainActivity:
#Override
public void setAdapter(boolean isUserExist) {
Log.d("RegDebug", "In setAdapter");
mainPagerAdapter.clearData();
mainPagerAdapter.addFragment(searchFragment, getString(R.string.search_title));
if (isUserExist) {
Log.d("RegDebug", "In setAdapter reg");
mainPagerAdapter.addFragment(new ChatsFragment(), getString(R.string.chats_title));
mainPagerAdapter.addFragment(new ActionsFragment(), getString(R.string.actions_title));
Toast.makeText(getApplicationContext(), "Registered!", Toast.LENGTH_SHORT).show();
} else {
Log.d("RegDebug", "In setAdapter unreg");
mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.CHATS_TAB_NAME), getString(R.string.chats_title));
mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.ACTIONS_TAB_NAME), getString(R.string.actions_title));
Toast.makeText(getApplicationContext(), "Unregistered!!!", Toast.LENGTH_SHORT).show();
}
mainPagerAdapter.notifyDataSetChanged();
viewPager.setAdapter(mainPagerAdapter);
}
I call this method in presenter with setting value from firebase auth, checking if user exists:
public void checkForUserExist() {
if (mainInteractor.isUserExist()) {
getViewState().setRegAdapter();
} else getViewState().setUnregAdapter();
}
And then call presenter method in onCreate of MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialogFragment = new FilterDialogFragment();
searchFragment = new SearchFragment();
//UI
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.main_view_pager);
tabLayout = findViewById(R.id.main_tabs);
//mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
mainPresenter.checkForUserExist();
tabLayout.setupWithViewPager(viewPager);
}
I try to log the boolean result and it returns exactly value that must be, but pager adapter can't update its content.Code of MainPagerAdapter:
public class MainPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentTitleList.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
public void clearData(){
fragmentList.clear();
fragmentTitleList.clear();
notifyDataSetChanged();
Log.d("RegDebug", " fragmentList size is " + fragmentList.size()
+ " fragmentTitleList size is " + fragmentTitleList.size());
}
}
Use OnPageChangeListener of ViewPager class & notify to your current fragment from there using interface.

Fragment Listener doesn't works when Activity is destroyed

I make chat application with android and socket. Chat interface is inside the fragment. While both device show chat interface, it runs well. But when one of device close, it give me error force close.
Here is my code.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FragmentManager manager;
private android.support.v4.app.FragmentTransaction transaction;
private Socket socket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
chatFragment enter = new chatFragment();
transaction.add(R.id.fragmentContainer, enter);
transaction.commit();
}
}
}
and ChatFragment.java
public class ChatFragment extends Fragment {
private ListView listchat;
private Button send;
private EditText msgText;
public ChatAdapter adapter;
private ArrayList<ChatModel> items = new ArrayList<ChatModel>();
private boolean isactive;
private Toolbar chatToolbar;
private FragmentManager manager;
private FragmentTransaction transaction;
private Socket socket;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View v;
v = inflater.inflate(R.layout.chat_fragment, container, false);
SocketSingleton singleton = (SocketSingleton) getActivity().getApplication();
socket = singleton.getmSocket();
// this is listener for message coming
socket.on("message", messageListener);
msgText = (EditText) v.findViewById(R.id.editMsg);
adapter = new ChatAdapter(getActivity(), R.layout.chat_list, items);
listchat = (ListView) v.findViewById(R.id.listchat);
listchat.setAdapter(adapter);
listchat.setDivider(null);
send = (Button) v.findViewById(R.id.btnSend);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMessage();
}
});
return v;
}
private void addMessage() {
ChatModel item = new ChatModel();
item.setMsg(msgText.getText().toString());
items.add(item);
adapter.notifyDataSetChanged();
String message = msgText.getText().toString();
socket.emit("message", message);
msgText.setText("");
}
private Emitter.Listener messageListener = new Emitter.Listener() {
#Override
public void call(final Object... args) {
// error start from here
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject json = (JSONObject) args[0];
String msg;
try {
msg = json.getString("message").toString();
ChatModel model = new ChatModel();
model.setMsg(msg);
items.add(model);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
};
}
In longcat it says.
FATAL EXCEPTION: EventThread
java.lang.NullPointerException
in application, it force close when message is coming and activity close. How to handle this listener when message is coming, and activity close? Thanks.
You should check for getActivity() first .
if( getActivity() != null ){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject json = (JSONObject) args[0];
String msg;
try {
msg = json.getString("message").toString();
ChatModel model = new ChatModel();
model.setMsg(msg);
items.add(model);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
You have to set messageListener to null in onDetach() of your fragment.
#Override
public void onDetach() {
super.onDetach();
messageListener = null;
}

Tabs with Fragment life cycle

in my app i have a Tabs with fragment classes. i trying to refresh listView in "fragment a" with the new data i insert.
In "fragment b" i have EditText and Button ,that inserting text to the database.
in "fragment a" i have the ListView with the dataBase rows. when "fragment a" is "OnCreateView" i just put the dataBase on a "ArrayList" and past it to my baseAdapter.
but "onCreateView" not refreshing my new data every time i get into the "fragment a" else i goes to "fragment c" and "onDestroy" call on "fragment a".
so my result it was to call : "setUserVisibleHint" override method, and check if it is visible and refreshing the list.
but i dont think it is the good practice .
what should i do?
Class a
public class ListFragment extends Fragment{
basAdapterCustom adapter;
ListView lv;
ArrayList<Clock> list;
private DbHandler hand;
Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_fragment, container, false);
context = getActivity();
Log.d(TAG, "onCreateView");
hand = new DbHandler(context);
list = new ArrayList<Clock>();
lv = (ListView) v.findViewById(R.id.listOfShifts);
adapter = new basAdapterCustom(list, getActivity());
lv.setAdapter(adapter);
refreshList();
return v;
}
//like on "resume":
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Make sure that we are currently visible
if (this.isVisible()) {
refreshList();
if (!isVisibleToUser) {
// TODO stop
}
}
}
private void refreshList() {
list = hand.getByWorkName();
adapter = new basAdapterCustom(list,getActivity());
lv.setAdapter(adapter);
}
class b:
public class ClockFragment extends Fragment{
DbHandler hand;
Context context;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.clock, container,false);
context = getActivity();
hand = new DbHandler(context);
return v;
}
// ADD to .Db
public void addToDb(View v){
hand.add(new Clock(0, dateDay));
}
}
class mainActivity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener, OnPageChangeListener{
public static final String TAG = "myClock";
String[] tabMenu = {"FRAG A","FRAG B","FRAG C"};
private ViewPager viewPager;
private TabPagerAdapter mAdapter;
private ActionBar actionBar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(MainActivity.TAG, "OnCreate = MainActivity (Pager");
viewPager = (ViewPager) findViewById(R.id.pager );
actionBar = getActionBar();
mAdapter = new TabPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tabsNames : tabMenu) {
actionBar.addTab(actionBar.newTab().setText(tabsNames).setTabListener(this));
}
viewPager.setOnPageChangeListener(this);
}
// public void transDialog(){
// Dialog mDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
// }
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
}
PagetAdapter.class
public class TabPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = "myClock";
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int index) {
Log.d(TAG, " CLASS : TabPagerAdapter");
switch (index) {
case 0:
return new ListFragment();
case 1:
return new ClockFragment();
case 2:
return new SettingFragment();
default:
break;
}
Log.d(TAG, " CLASS : TabPagerAdapter = "+index);
return null;
}
#Override
public int getCount() {
return 3;
}
}
Please help me i hope you are understand my problem...
You want to update listView in fragment A, after insert data in fragment b? Then  you have a few solutions to choose:
1. Implement communications between fragments via activity. Then B insert date, it send message to activity, that data need to be updated. Then fragment A started, it's ask activity to need update data. For details on implementing this communication check link.
2. Use some bus library, EventBus for example. Then fragment B insert data, it post 'data changed' event to bus. Fragment A checks on start if this event occurs.
3. Use Loaders which "monitor the source of their data and deliver new results when the content changes."

Asynctask in another class?

I have a problem with the call of asyncTask....
i have a class named arrivi
public class arrivi extends Fragment {
ListView list;
int thread = 0;
public String[] lista = new String[200];
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.arrivi, container, false);
new MyTask().execute("");
return ios;
}
//INIZIO THREAD
public class MyTask extends AsyncTask<String, Void, String> {
with new MyTask this work fine...
but i want call MyTask into MainActivity.java
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.....
.....
i have tried to
arrivi a = new arrivi();
a.MyTask.execute("");
i don't know...
how to make a call?? can you make me an example of code?
Thank u guys!
There are two ways to do this, but what it looks like you are trying to do here is actually to make MyTask static:
public static class MyTask extends AsyncTask<String, Void, String> {
and that way, in your activity, you can just call:
new arrivi.MyTask().execute();
(Optional) further explanation:
the other alternative is to use the syntax -
new arrivi().new MyTask().execute(); which i think is what you could also be trying to accomplish.
given that you are calling your MyTask in two separate places, it seems you might as well either make your MyTask static OR place MyTask in its own class outside of both of these two classes (in order to syntactically make this correct. This, however, depends on if you want MyTask to interact with your fragment arrivi. For example, if, in your onPostExecute() of MyTask, you want to access something that arrivi has (such as a view), you probably want to place MyTask inside of arrivi still, and not use the static declaration
thank u for replies... in mainactivity i have this:
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Info").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Arrivi").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Partenze").setTabListener(tabListener));
if (isOnline()) {
//System.out.println("CONNESSIONE OK");
new arrivi().new MyTask().execute();
}else{
try {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Connessione Internet non disponibile.");
alertDialog.setButton("Esci", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
catch(Exception e) { }
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
}
and this is arrivi class:
public class arrivi extends Fragment {
ListView list;
int thread = 0;
public String[] lista = new String[200];
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.arrivi, container, false);
new MyTask().execute("");
return ios;
}
//INIZIO THREAD
public class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String info;
#Override
protected void onPreExecute() {
prog = new ProgressDialog(getActivity());
prog.setMessage("Connessione in corso...");
prog.show();
prog.setCanceledOnTouchOutside(false);
prog.setCancelable(false);
}
#Override
protected String doInBackground(String... params) {
try {
org.jsoup.nodes.Document doc = Jsoup.connect("http://site.eu").timeout(7*1000).get();
org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();
while(iterator.hasNext()){
thread++;
lista[thread] = iterator.next().text();
System.out.println("Posizione["+thread+"]"+lista[thread]);
}
}catch (IOException e) {
e.printStackTrace();
}
return info;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
CheckRow();
}
}// FINE THREAD
but when i lunch my app, crashes and here there is a logcat
http://paste.ubuntu.com/7253904/
i think line 43 is NULL... prog = new ProgressDialog(getActivity());
..thank you so much guys!!

Categories

Resources