主类扩展Fragment并实现onclicklistner。

此代码段来自我的异步任务。

关于如何在片段类中获取应用程序文本的任何想法?

据我了解,某个地方存在计时/通话问题,这会导致内存泄漏。我是否必须声明一个Context变量?

    @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


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

最佳答案

替换此行-

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

与-
pd = new ProgressDialog(getActivity());

因为仅在您所在的活动中需要进度对话框。

10-07 20:28