1

The main class extends Fragment and Implements onclicklistner.

this snippet is from my async task.

Any ideas of how to get the application text within fragment classes?

As far as I understand there is a timing/calling issue somewhere which causes the memory leak. Would I have to declare a Context variable maybe?

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.tracklist_fragment_view_fragment, container, false);

    Button b = (Button) rootView.findViewById(R.id.sendtofriend);
    b.setOnClickListener(this);

    return rootView;
}

@Override
public void onClick(View v) {
    new GetDataTask().execute();
}       

@SuppressWarnings("unused")
private class GetDataTask extends AsyncTask<Void, Void, Void> {         

    @Override
    protected void onPreExecute() {         
        pd = new ProgressDialog(getActivity().getApplicationContext());
        pd.setTitle("Grabbing Track!");
        pd.setMessage("Please wait...");
        pd.setCancelable(false);
        pd.setIndeterminate(true);
        pd.show();
    }

LOGCAT >>>

01-13 10:15:01.511: W/dalvikvm(11829): threadid=1: thread exiting with uncaught exception (group=0x4163a970)
01-13 10:15:01.521: E/AndroidRuntime(11829): FATAL EXCEPTION: main
01-13 10:15:01.521: E/AndroidRuntime(11829): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:623)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.app.Dialog.show(Dialog.java:286)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at com.superbit.swipeexperiment.TrackListFragment$GetDataTask.onPreExecute(TrackListFragment.java:79)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.os.AsyncTask.execute(AsyncTask.java:534)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at com.superbit.swipeexperiment.TrackListFragment.onClick(TrackListFragment.java:64)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.view.View.performClick(View.java:4280)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.view.View$PerformClick.run(View.java:17984)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.os.Handler.handleCallback(Handler.java:730)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.os.Looper.loop(Looper.java:158)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at android.app.ActivityThread.main(ActivityThread.java:5789)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at java.lang.reflect.Method.invoke(Method.java:525)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
01-13 10:15:01.521: E/AndroidRuntime(11829):    at dalvik.system.NativeStart.main(Native Method)
01-13 10:15:02.923: D/Process(11829): killProcess, pid=11829
01-13 10:15:02.923: D/Process(11829): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:123 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

code relating to getActivity();

            HttpResponse response = httpclient.execute(httppost);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            final String responseServer = httpclient.execute(httppost,
                    responseHandler);

            final String formattedTrack = responseServer.trim().replaceAll(
                    "\\s+", " ");

            parts = formattedTrack.split("\\=",-1);
            final String trackName = parts[0].toString();
            final String youtube = parts[1].toString();

            values.add(trackName);                              

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {         
        SetListViewAdapter();
        pd.dismiss();
    }

    private void SetListViewAdapter() {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(), R.layout.custom_listview, values);
        lv = (ListView) getView().findViewById(R.id.listView1);
        lv.setAdapter(adapter);
        if (values.size() == 2) {
            values.remove(0);
        }

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {


                VideoFragment f = VideoFragment.newInstance(parts[1].toString());
                getFragmentManager().beginTransaction()
                        .replace(R.id.youtube_player_frag, f).commit();
            }
        });
    }
}
Jesson Atherton
  • 644
  • 7
  • 21
  • 1
    See http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998 – laalto Jan 13 '14 at 10:26

2 Answers2

0

Replace this line-

pd = new ProgressDialog(getActivity().getApplicationContext());

with-

pd = new ProgressDialog(getActivity());

beacuse you need the progressdialog only in the Activity you are in.

amit singh
  • 1,407
  • 2
  • 16
  • 25
  • Originally I was using getActivity() however after a few cycles through the code it causes a memory leak. this is the logcat... – Jesson Atherton Jan 13 '14 at 10:40
  • 01-13 10:38:51.089: E/WindowManager(13841): Activity com.superbit.swipeexperiment.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42fafb10 V.E..... R......D 0,0-1026,486} that was originally added here – Jesson Atherton Jan 13 '14 at 10:40
  • leaked window error comes when you finishes your activity before finishing your progressDialog. So, first dismiss the progressDialog and then finish the activity related to that. – amit singh Jan 13 '14 at 10:50
  • This is where my problem lies, I dismiss the progress dialog in the on post execute method. I have tried changing where the code is executed but still the same issue. – Jesson Atherton Jan 13 '14 at 11:00
  • @JessonAtherton show your that code and log related to that problem – amit singh Jan 13 '14 at 11:04
  • there is the rest of the class including the list view class. I cant put the whole class because it's for a production app. – Jesson Atherton Jan 13 '14 at 11:09
  • call SetListViewAdapter() after pd.dismiss(); – amit singh Jan 13 '14 at 11:13
  • 01-13 11:15:59.747: E/WindowManager(17907): Activity com.superbit.swipeexperiment.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41f23c50 V.E..... R......D 0,0-1026,486} that was originally added here – Jesson Atherton Jan 13 '14 at 11:17
0

I'm not sure about this,

write the code in a UI thread and make pd static to avoid leakage

public static ProgressDialog pd;

getActivity().runOnUiThread(new Runnable() {

@Override                                               
public void run() {
     pd = new ProgressDialog(getActivity().getApplicationContext());
            pd.setTitle("Grabbing Track!");
            pd.setMessage("Please wait...");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
    }
});
Athul
  • 231
  • 1
  • 7