您好,我正面临这个问题:我正在使用兼容包在Android应用程序中使用片段(min SDK 2.1).
片段有时会发生随机异常,我无法弄清楚原因.这是我收到的堆栈跟踪:
java.lang.IllegalStateException: Fragment FeedListFragment{438a54e8} not attached to Activity
at android.support.v4.app.Fragment.getLoaderManager(Fragment.java:715)
at com.myappli.ui.FeedListFragment.refreshUpdateDate(FeedListFragment.java:283)
at com.myappli.ui.phone.FeedListActivity.onReceiveResult(FeedListActivity.java:277)
at com.myappli.data.rssplayer.service.KTDetachableResultReceiver.onReceiveResult(KTDetachableResultReceiver.java:55)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:4425)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
这是我在Fragment类中调用的相应代码:
public class FeedListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>, FeedListCursorAdapterListener {
...
public void refreshUpdateDate() {
getLoaderManager().restartLoader(LAST_UPDATE_CURSOR_ID, null, this);
}
...
}
Run Code Online (Sandbox Code Playgroud)
以下是调用片段的活动的代码:
private FeedListFragment mCursorLoaderListFg;
if (!isFinishing()) {
mCursorLoaderListFg.refreshUpdateDate();
mCursorLoaderListFg.refreshDisplay();
mCursorLoaderListFg.hideLoadingArticles();
}
Run Code Online (Sandbox Code Playgroud)
这是getLoaderManager()的片段源代码:
/**
* Return the LoaderManager for …
Run Code Online (Sandbox Code Playgroud) 我在ICS上测试我的应用程序时遇到了一个奇怪的问题.
在Android 2.X上使用以下代码效果很好(有时会发生超时但很少次):
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);
final DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
// Create a new HttpClient and Post Header
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
if (keys != null) {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
}
// Cookies
// Create a local instance of cookie store
if (checkCookieValues()) {
BasicClientCookie cookieSession = new BasicClientCookie(mCookieName, mCookieValue);
cookieSession.setDomain(mCookieDomain);
httpClient.getCookieStore().clear();
httpClient.getCookieStore().addCookie(cookieSession);
}
// Execute HTTP Post Request
response = httpClient.execute(httpPost);
httpClient.getConnectionManager().shutdown();
} catch …
Run Code Online (Sandbox Code Playgroud)