我在这里看到了这个问题,但没有解决我的问题。我的片段扩展了一个名为CommonListFragment
的类,它扩展了Fragment类。我托管片段的Activity扩展了一个名为CommonActivity
的类,它扩展了FragmentActivity
类。一切似乎都没问题,为什么会出现以下错误?
Android
ClassCastException
列表无法转换为android.app.Fragment
正如您可以看到的那样,Android正在尝试将我的List类转换为android.app.Fragment
,而它应该真正转换为android.support.v4.app.Fragment。 My List类最终扩展了Fragment类(android.support.v4.app.Fragment)。我的最小和目标SDK也是Android 2.2。所以,如果我尝试导入'android.app.Fragment',则会出现编译错误。那么我如何强制Android将我的List识别为android.support.v4.app.Fragment?
CommonActivity.java
:
public class CommonActivity extends FragmentActivity {
...
}
ListsContainer.java
:
public class ListsContainer extends CommonActivity implements OnListChangedListener {
private int m_orientation ;
private static int EXTRA_LARGE = 4;
@Override
public void onCreate(Bundle savedInstanceState) {
m_ctx = this;
setContentView(R.layout.lists_container);
...
CommonListFragment
:
public class CommonListFragment extends Fragment implements IOnEditTextClickedListener,
IOnItemCheckedListener,
IOnDismissDialog,
ISettings,
OnClickListener
{
public void onItemChecked(int pos,boolean checked,String sGroup,String sList){
}
...
}
lists_container.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/listsFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.onesix.myApp.Lists" ></fragment>
<fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.onesix.myApp.List" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
logcat的
08-06 00:15:44.228: W/ServerManagedPolicy(7472): Licence retry count (GR) missing, grace period disabled
08-06 00:17:35.207: W/dalvikvm(7472): threadid=1: thread exiting with uncaught exception (group=0x41814970)
08-06 00:17:35.287: E/AndroidRuntime(7472): FATAL EXCEPTION: main
08-06 00:17:35.287: E/AndroidRuntime(7472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onesix.myApp/com.onesix.myApp.ListsContainer}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread.access$600(ActivityThread.java:162)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.os.Looper.loop(Looper.java:158)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread.main(ActivityThread.java:5789)
08-06 00:17:35.287: E/AndroidRuntime(7472): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 00:17:35.287: E/AndroidRuntime(7472): at java.lang.reflect.Method.invoke(Method.java:525)
08-06 00:17:35.287: E/AndroidRuntime(7472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-06 00:17:35.287: E/AndroidRuntime(7472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
08-06 00:17:35.287: E/AndroidRuntime(7472): at dalvik.system.NativeStart.main(Native Method)
08-06 00:17:35.287: E/AndroidRuntime(7472): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-06 00:17:35.287: E/AndroidRuntime(7472): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:339)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Activity.setContentView(Activity.java:1944)
08-06 00:17:35.287: E/AndroidRuntime(7472): at com.onesix.myApp.ListsContainer.onCreate(ListsContainer.java:18)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Activity.performCreate(Activity.java:5195)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
08-06 00:17:35.287: E/AndroidRuntime(7472): ... 11 more
08-06 00:17:35.287: E/AndroidRuntime(7472): Caused by: java.lang.ClassCastException: com.onesix.myApp.Lists cannot be cast to android.app.Fragment
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Fragment.instantiate(Fragment.java:617)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Fragment.instantiate(Fragment.java:592)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.app.Activity.onCreateView(Activity.java:4800)
08-06 00:17:35.287: E/AndroidRuntime(7472): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
List.java
:
public class List extends CommonListFragment implements IOnDismissYesNoListener
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//setContentView(R.layout.list);
View view = inflater.inflate(R.layout.list,container, false);
//super.onCreate(savedInstanceState);
return view;
}
...
}
最新的LOGCAT
08-06 01:05:00.736: E/AndroidRuntime(15588): Caused by: java.lang.ClassCastException: com.onesix.myApp.Lists cannot be cast to android.app.Fragment
08-06 01:05:00.736: E/AndroidRuntime(15588): at android.app.Fragment.instantiate(Fragment.java:617)
08-06 01:05:00.736: E/AndroidRuntime(15588): at android.app.Fragment.instantiate(Fragment.java:592)
08-06 01:05:00.736: E/AndroidRuntime(15588): at android.app.Activity.onCreateView(Activity.java:4800)
08-06 01:05:00.736: E/AndroidRuntime(15588): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
有人有什么想法吗?我已经在SO上检查了关于这个主题的每个问题......
答案 0 :(得分:0)
<强>问题:强>
class="com.onesix.myApp.List"
在您的班级List
中,您正在导入支持库的片段而不是默认的Android片段,因为您使用的是FragmentActivity
,这意味着您必须使用非支持库中的片段
确保所有片段类特别在List class
导入此内容:
import android.app.Fragment;
编辑:
<fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:name="com.onesix.myApp.List" >
<!-- Preview: layout=@layout/details -->
</fragment>