使用意图从活动中启动fragmentActivity

时间:2012-12-04 04:15:07

标签: android android-intent android-fragments

我有一个主要的活动。我有一些动作按钮。我想当我按下一个按钮然后将开始一个包含两个片段的新活动。但是当我点击按钮时它会给我错误。我通过意图从活动导航到另一个活动。这是我的意图代码

if(v.getId()==R.id.Search)
{
    Intent i=new Intent(MainActivity.this,Search.class);
    startActivity(i);
}

这是我的日志文件

12-04 09:32:43.501: E/Trace(768): error opening trace file: No such file or directory (2)
12-04 09:32:46.812: E/AndroidRuntime(768): FATAL EXCEPTION: main
12-04 09:32:46.812: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.student_managment/com.example.student_managment.Search}: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-04 09:32:46.812: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 09:32:46.812: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
12-04 09:32:46.812: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-04 09:32:46.812: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-04 09:32:46.812: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
12-04 09:32:46.812: E/AndroidRuntime(768): Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-04 09:32:46.812: E/AndroidRuntime(768):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.Activity.setContentView(Activity.java:1867)
12-04 09:32:46.812: E/AndroidRuntime(768):  at com.example.student_managment.Search.onCreate(Search.java:13)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.Activity.performCreate(Activity.java:5008)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-04 09:32:46.812: E/AndroidRuntime(768):  ... 11 more
12-04 09:32:46.812: E/AndroidRuntime(768): Caused by: java.lang.ClassCastException: com.example.student_managment.Lower_frag cannot be cast to android.support.v4.app.Fragment
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
12-04 09:32:46.812: E/AndroidRuntime(768):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
12-04 09:32:46.812: E/AndroidRuntime(768):  ... 21 more

1 个答案:

答案 0 :(得分:0)

错误是Caused by: java.lang.ClassCastException: com.example.student_managment.Lower_frag cannot be cast to android.support.v4.app.Fragment,这意味着您可能声明了一个片段(在您的Search.class中填充的xml布局中)不是片段。检查Search.class中填充的布局