gpt4 book ai didi

java - (Android) 使用 getActivity() 会导致内存泄漏,但添加 get 应用程序上下文会导致错误 token 异常

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:32 26 4
gpt4 key购买 nike

主类扩展了 Fragment 并实现了 onclicklistner。

此 fragment 来 self 的异步任务。

关于如何在 fragment 类中获取应用程序文本有什么想法吗?

据我了解,某处存在计时/调用问题,导致内存泄漏。我可能必须声明一个 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());

因为您只需要在您所在的 Activity 中使用进度对话框。

关于java - (Android) 使用 getActivity() 会导致内存泄漏,但添加 get 应用程序上下文会导致错误 token 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21088815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com