8. wrong 2nd argument type found 'android.app.Fragment.' required 'Android.support.v4.app.Fragment'
rt,使用fragment的时候报以上错误。
<code class="hljs ocaml has-numbering">wrong <span class="hljs-number">2</span>nd argument <span class="hljs-class"><span class="hljs-keyword">type</span> <span class="hljs-title">found</span></span> <span class="hljs-string">'android.support.v4.app.Fragment.'</span> required <span class="hljs-string">'Android.app.Fragment'</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>
解决了半天!发现错误其实很简单,首要的是解读错误信息:第二个参数类型为android.app.Fragment,要求Android.app.support.v4.Fragment类型(其实就是参数类型错误)
Fragment是界面的一部分,经常被用于滑动界面,可以将多个fragment组合用于一个activity中,它具有生命周期和事件。
Fragment的包为 android.app.Fragment, 但是由于其对3.0下版本不兼容,所以要 引入 android.support.v4.app.Fragment 两个包对Fragment相同方法的定义有所不同,因此在import时,经常容易混淆导致程序报错!!!(以下是我的部分代码)
<code class="hljs avrasm has-numbering"> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.v</span>4<span class="hljs-preprocessor">.app</span><span class="hljs-preprocessor">.FragmentManager</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.v</span>4<span class="hljs-preprocessor">.app</span><span class="hljs-preprocessor">.FragmentTransaction</span><span class="hljs-comment">;</span> import <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.gym</span><span class="hljs-preprocessor">.zgbj</span><span class="hljs-preprocessor">.Fragment</span><span class="hljs-preprocessor">.LeftMenuFragment</span><span class="hljs-comment">;</span> import <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.gym</span><span class="hljs-preprocessor">.zgbj</span><span class="hljs-preprocessor">.Fragment</span><span class="hljs-preprocessor">.ContentFragment</span><span class="hljs-comment">;</span> private void initFragment() { FragmentManager fm = getSupportFragmentManager()<span class="hljs-comment">;</span> FragmentTransaction transaction = fm<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;// 开始事务</span> transaction<span class="hljs-preprocessor">.replace</span>(R<span class="hljs-preprocessor">.id</span><span class="hljs-preprocessor">.fl</span>_left_menu, new LeftMenuFragment(), TAG_LEFT_MENU)<span class="hljs-comment">;// 用fragment替换帧布局;参1:帧布局容器的id;参2:是要替换的fragment;参3:标记</span> transaction<span class="hljs-preprocessor">.replace</span>(R<span class="hljs-preprocessor">.id</span><span class="hljs-preprocessor">.fl</span>_main, new ContentFragment(), TAG_CONTENT)<span class="hljs-comment">;</span> transaction<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;// 提交事务</span> // Fragment fragment = // fm<span class="hljs-preprocessor">.findFragmentByTag</span>(TAG_LEFT_MENU)<span class="hljs-comment">;//根据标记找到对应的fragment</span> } </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li></ul>
<code class="hljs avrasm has-numbering"> transaction<span class="hljs-preprocessor">.replace</span>(R<span class="hljs-preprocessor">.id</span><span class="hljs-preprocessor">.fl</span>_left_menu, new LeftMenuFragment(), TAG_LEFT_MENU)<span class="hljs-comment">;</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>
在transaction.replace这一条语句报了错!其实看以上代码并发现不了错误,我导入的是android.support.v4.app包,而且在获取FragmentManager 使用的也是getSupportFragmentManager函数。一步步推理,
最终发现是LeftMenuFragment继承的Fragment,导入的是Android.app.Fragment包!
所以错误还是导错包!!!!!!!(泪奔)
总结
1.如果你导入的是Android.app.Fragment’包(即本地的jar包) import android.app.Fragment;
那么在获得FragmentManager时,使用的应该是 FragmentManager fm = getFragmentManager();
,对应的fragment也要导入对应的包!
2.如果你导入的是android.support.v4.app.Fragment.包(即v4包) import android.support.v4.app.Fragment;
那么在获得FragmentManager时,使用的应该是 FragmentManager fm = getSupportFragmentManager();
对应的fragment也要导入对应的包!
一定要注意你所继承的fragment,导入的是哪个包!
这个讲解了fragment,学习学习
http://blog.csdn.net/qq_16444837/article/details/48183877