我原本以爲我的轉換片段非常好做,直到我旋轉屏幕。我收到以下類型的錯誤:Android的碎片重新實例化錯誤
RuntimeException:無法啓動活動ComponentInfo {com.ghcssoftware.gedstar/com.ghcssoftware.gedstar.GedStar}:android.support.v4.app.Fragment $ InstantiationException:Unable to實例化片段com.ghcssoftware.gedstar.PersonTab $ PersonTabFrag:確保類名存在,是公共的,並且具有公共的空構造函數
有問題的類確實存在,是公共的,並且我添加了一個空構造函數結果沒有變化。看一些示例代碼,我注意到與我的代碼編寫方式有一些區別,儘管我沒有看到空構造函數:
1)是否有任何理由將我的片段類聲明爲「static」很多樣本是?
2)我需要我的片段類中實現使用「的newInstance」的?爲什麼這樣做,而不是隻有一個構造函數?例如從V14樣本之一:
public static class CountingFragment extends Fragment {
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static CountingFragment newInstance(int num) {
CountingFragment f = new CountingFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
我還是有點生疏了一些Java概念,所以可能失去了一些東西基本。
道格·戈登 GHCS軟件
我不知道什麼與您的旋轉問題的發生,但這頁面描述靜態類聲明的目的是:http:/ /www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html – goto10